How rail routing works

In my route file, I have a route as shown below. What does it mean? I looked in the rails guide route, but I cannot find an explanation for => and foo: 'bar'

get '/clients/:status' => 'clients#index', foo: 'bar'

Please explain what does this mean?

+4
source share
2 answers

First, I assume that your web server is running on the host and port localhost:3000. But this part is not important - no matter which host and port receive the request to Rails, it is all the same.

get '/clients/:status' => 'clients#index', foo: 'bar'

If you are not familiar with Ruby's syntactic sugar, this is equivalent to:

get({'/clients/:status' => 'clients#index', :foo => 'bar'})

From here, let me break it in pieces:

  • get means that the route applies only to HTTP GET requests.
  • '/clients/:status' http://localhost:3000/clients/:status, :status -
  • => 'clients#index' Rails, ClientsController index.
  • :foo => 'bar', , - , ( vee), ( ) params, ClientsController s .

Rails . Rails, IMHO:

http://guides.rubyonrails.org/routing.html

+3

, HTTP- GET URI /clients/:status, :status - .

=> clients#index Controller#action, controller clients i.e ClientsController action index.

, foo: 'bar', . , as, constraints ..

, as ( ):

get '/clients/:status' => 'clients#index', as: :client_status

as: :client_status foo: 'bar'.

+4

All Articles