1

In ruby on rails, what does the "as:" do in route?

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

You can also generate paths and URLs. If the route above is modified to be:

get '/patients/:id', to: 'patients#show', as: 'patient'

and your application contains this code in the controller:

@patient = Patient.find(17)

and this in the corresponding view:

<%= link_to 'Patient Record', patient_path(@patient) %>
peipei
  • 1,407
  • 3
  • 14
  • 22

3 Answers3

6

In routes as: option is used to make url or path helpers for that particular route. If you look at your route:

get '/patients/:id', to: 'patients#show', as: 'patient'

You have specified as: 'patient' which will enable rails to make patient_path and patient_url helpers

patient_path will give you /patient/:id and patient_url will give you domain/patient/:id

If you run rake routes in your terminal it will list all the routes of your application with there corresponding helper methods. For details checkout path and url helpers

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
Mandeep
  • 9,093
  • 2
  • 26
  • 36
1

It defines what the route helpers will look like:

patient_path(@patient)
Brad Werth
  • 17,411
  • 10
  • 63
  • 88
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

It's the path name which is generated from your routes

For example, if you have the following:

#config/routes.rb
resources :bones, as: :skeleton

You'll get routes as skeleton_path for the bones controller

--

You have to remember that Rails' routing structure is based around "resources" (handled by controllers). Your routes, therefore, should be structured around the controllers your application has

If you want to change the "name" of a particular controller path helper (from bones to skeleton for example), you'll be able to use the as option

Richard Peck
  • 76,116
  • 9
  • 93
  • 147