Ruby on rails routes - what does the "as" do? [duplicate] - ruby-on-rails

This question already has answers here:
:as in rails routes.rb
(3 answers)
Closed 8 years ago.
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) %>

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

It defines what the route helpers will look like:
patient_path(#patient)

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

Related

Including attributes in custom Rails routes

I hope the title is not to misleading, as I don't know a better title for the problem I'm working on:
I have a doctor which belongs to location and specialty. I'd like to route to show action of the doc controller like this:
/dentist/berlin/7
I defined my routes like this:
get ':specialty/:location/:id', to: 'docs#show'
And in my views create the following url to link to the show action of the doc controller:
<%= link_to doc.name, "#{doc.specialty.name}/#{doc.location.name}/#{doc.id}" %>
Is this a good solution to the problem? If not, is there a cleaner way to construct urls like this possibly using resources? What the heck is the name for a this problem?
Thank your very much for your help in advance.
For references, you should have a look at this page (especially the end of section 2.6)
If it is only for a single route, it's okay as you did. But then if you want to have more than one route (like /dentist/berlin/7, /dentist/berlin/7/make_appointment, etc.) you might want to structure a bit more your routes so as to take advantage of rails resources.
For example, instead of
get ':specialty/:location/:id', to: 'doctors#show'
get ':specialty/:location/:id/appointment', to: 'doctors#new_appointment'
post ':specialty/:location/:id/appointment', to: 'doctors#post_appointment'
You could have something like this (the code is almost equivalent, see explanation below)
resources :doctors, path: '/:specialty/:location', only: [:show] do
member do
get 'new_appointment'
post 'create_appointment'
end
end
Explanation
resources will generate the RESTful routes (index, show, edit, new, create, destroy) for the specified controller (doctors_controller I assume)
The 'only' means you don't want to add all the RESTful routes, just the ones specified
Then you want to add member actions, ie. actions that can be executed on a particular item of the collection. You can chose different syntaxes
resources :doctors do
member do
# Everything here will have the prefix /:id so the action applies to a particular item
end
end
# OR
resources :doctors do
get 'new_appointement', on: :member
end
By default, the controller action is the same as the path name you give, but you can also override it
member do
get 'appointment', action: 'new_appointment'
post 'appointment', action: 'post_appointment'
end
Rails has some wonderful helpers when it comes to routing !
The correct approach is to give your route a name, like this:
get ':specialty/:location/:id', to: 'docs#show', as: 'docs_show'
Then you can use it like this:
<%= link_to doc.name, docs_show_path(doc.specialty.name, doc.location.name, doc.id) %>
Note 1:
Rails appends _path at the end of the route names you define.
Note 2:
You can see all the available named routes by executing rake routes.

Cant create a link to named routes

I want to create a link to a named route
My routes.db have the following rule
match '/tablero', to: 'tablero#index',via: 'get' , as: 'tablero_main'
I can see the route using rake routes
tablero_main GET /tablero(.:format) tablero#index
But when i use the link_to as follows i get the "undefined local variable or method `tablero_main'" error.
<%= link_to "Tablero",tablero_main %>
Is there anything else i am missing?
You need to append path to the method name, like so:
<%= link_to "Tablero", tablero_main_path %>
Routes
To help you further, you'll need to also consider the role of resources in your routes
As Rails uses a resourceful routing infrastructure, every route you create should be based around a resource. In your case:
#config/routes.rb
resources :tablero, only: :index #-> domain.com/tablero
Admittedly, this will give you the path tablero_index_path, rather than tablero_main_path, but it ensures your routes are not only DRY, but also extensible. Nothing worse than having 100's of "match routes in a route file.
--
Helpers
After that, remember to use the correct route_path helper:
Each "route" path is basically just a helper method (which builds a URL for you). When using link_to, you need to reference the path helper directly. You didn't do this, which lead Rails to come back with the undefined method error

Migrating Routes to Rails 4

I am about to migrate my rails 3 application to rails 4.
There are some additional routes on my ressources that make some problems.
I get an error message for this lines in my route file:
resources :teams do
...
get 'apply' => 'teams#apply_membership', as: :apply_membership
post 'apply' => 'teams#apply_membership_save', as: :apply_membership
...
This is the generated error message
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
In rails3 it was possible to define a get and a post route using the same alias and routing them to different controler methods.
Can I do this in rails4, too?
And if yes, how does it have to look like in my route file?
You can not take two route name with same name. but you have done it. so please change,
get 'apply' => 'teams#apply_membership', as: :apply_membership
post 'apply' => 'teams#apply_membership_save', as: :update_membership
Take a look here for rails routings. http://guides.rubyonrails.org/routing.html

How did Rails generate this path from our model and controller

Ok I kind of understand this part: CRUD verbs and actions http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
and if I go to the route file of the example I have, I also see a resources :orders in it.
But now in the view of a partial names _carts I am seeing this code:
<%= button_to "Checkout" , new_order_path, method: :get %>
What confuses me is the new_order_path ? Where did that come from? What Rails convention rule is allowing us to right this? Especially where did that "new" come from?
When you use resources :orders in the routes, Rails creates 7 routes for new, create, show, update, destroy, list, and edit. All of them are given names, and new_order_path/new_order_url is related to the new action.
These routes are described at the http://guides.rubyonrails.org/routing.html#paths-and-urls
Those path helpers are automatically generated for resources defined in your routes.rb. You can check what route helpers are available by executing rake routes at the command line. They are shown in the left-most column in the table that prints out.
The general pattern of the paths that are created are like so by default:
new_{singular form of resource}_path - Routes to new on GET
edit_{singular form of resource}_path - Routes to edit on GET
{singular form of resource}_path - Routes to show on GET, destroy on DELETE, update on PUT (Soon to be PATCH in Rails 4)
{plural form of resource}_path - Routes to index on GET and create on POST.
There's also helpers that end in _url instead of _path that provide absolute URLs instead of relative paths. The particular action that is hit in your controller depends on the HTTP verb (GET, PUT, POST, DELETE, etc.) used when visiting those URLs.

Why do some of my Rails path helpers have an _index suffix? [duplicate]

This question already has answers here:
Rails 3 route appends _index to route name
(3 answers)
Closed 8 years ago.
I have a Rails route definition that looks something like this:
namespace :admin do
resources :feeds
resources :push
end
rake routes generates the following output for it:
admin_feeds GET /admin/feeds {:controller=>"admin/feeds", :action=>"index"}
admin_push_index GET /admin/push {:controller=>"admin/push", :action=>"index"}
Why would would the path helper for push get the _index suffix, but not feeds?
It's all based on the plurality of the resource. So if the resource name is plural, then it has no need to add an _index suffix since it's inferred.
If it is a singular resource name, then it adds the suffix for clarification since admin_push would typically be a show action instead of the index action.
You can also use
resource :push
instead of
resources :push
to specify a singular resource. See http://api.rubyonrails.org/classes/ActionDispatch/Routing.html

Resources