Rail routes

I am creating a route in which there should be a URL of something like http://mysite/cars/1/similar/ , which will have all the cars that look like a car with the specified id (in this case 1)

I saw that you can create rails element routes in the routes.rb file with the syntax

 resources :cars do member do get :similar end end 

I can also do something like

 match 'cars/:id/similar' => 'cars#similar', :via => "get 

What is the difference between these two syntaxes

+4
source share
2 answers

One of them is standardized in default route advertisements, and IMO is easier to find. The other is not, which can lead to typos, etc. Not very important, but for RESTful actions, I prefer to use a resourceful mechanism.

You can also use the single-line version, which I prefer for individual routes:

 resources :cars do get :similar, :on => :member end 

Meagar is correct, I forgot that the matching form will not create helper methods.

+7
source

Two methods are not equivalent.

The first method creates a helper method similar_car . The second method does not work.

The helper method is important if you intend to do things like

 = link_to "Similar", similar_car_path(@car) 

To make them equivalent, you need to provide the option :as :

 get "cars/:id/similar" => "cars#similar", :as => "similar_car" 
+8
source

All Articles