I am asking this question cause i guess i have not good understanding over how rails params works.
i want to know how the path variable goes to rails params or mapped to key and value in rails params.
get 'x/y/:id'
if i hit the url localhost:3000/x/y/2, here 2 will goes to params with key id: like params[id: 2]. How does it. Can anyone please help here to understand it.
CodePudding user response:
The URL http://localhost:3000/x/y/2
results in a request to port 3000 on your Rails server something like this:
GET /x/y/2 http/1.1
Host: localhost
The server receives the request and breaks it down into its parts.
- scheme: http
- version: 1.1
- host: localhost
- port: 3000
- path: /x/y/2
- method: GET
Rails compares the path to your routes, and sees it matches get '/x/y/:id'
. 2 is set as params[:id]
. Then it calls the controller method specified in by the route.
For more read Rails Routing from the Outside In and HTTP 1.1 Request Messages.
CodePudding user response:
Somehow the answer by @schwern is right but actual thing is happen under hood Rails.application.routes.recognize_path '/account/1', method: :get this will convert your path to {:controller=>"books", :action=>"check", :id=>"1"}