Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 route appends _index to route name

I am migrating a Rails 2.3.8 version to Rails 3.0 and so ive rewritten my routes file. When i list the routes using rake routes, i see some route names have _index appended to them. I cant figure out why this is.

Relevant routes:

Rails 2.3.8:

map.namespace "tracker", :path_prefix => "" do |planner|     planner.resources :planner, :collection => {:step1 => :get,                                                 :add => :get,                                                 :unsubscribe => [:get, :post] } end 

Rails 3.0 route:

namespace "tracker", :path => "" do   resources :planner do     collection do       get :step1       get :add       get :unsubscribe       post :unsubscribe     end   end end 

Output from rake routes

Rails 2.3.8

step1_tracker_planner        GET    /planner/step1(.:format) add_tracker_planner          GET    /planner/add(.:format) unsubscribe_tracker_planner  GET    /planner/unsubscribe(.:format)                              POST   /planner/unsubscribe(.:format)  

Rails 3.0

step1_tracker_planner_index       GET    /planner/step1(.:format) add_tracker_planner_index         GET    /planner/add(.:format) unsubscribe_tracker_planner_index GET    /planner/unsubscribe(.:format)                                   POST   /planner/unsubscribe(.:format)  

Any ideas as to why this _index is being added would be much appreciated.

like image 868
zsquare Avatar asked Jun 25 '11 08:06

zsquare


People also ask

How do I find routes in Rails?

Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

How do you define a route in Ruby?

We have to define the routes for those actions which are defined as methods in the BookController class. Open routes. rb file in library/config/ directory and edit it with the following content. The routes.

What is match in Rails routes?

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.


2 Answers

It is because your resource is named :planner instead of :planners that Rails decided to add the _index to any collection nested underneath. My guess it is there for readability.

The action named in the collection normally translates to a verb, so I can see why this makes sense. Take the typical photos resource example given in the routing docs:

resources :photos do   collection do     get 'search'   end end  search_photos GET    /photos/search(.:format) 

But if instead we called the resources 'photo'...

resources :photo do   collection do     get 'search'   end end  search_photo_index GET    /photo/search(.:format) 

In the first case, you search the "photos", and in the second case you search the "photo index".

like image 191
Aaron Hinni Avatar answered Oct 11 '22 23:10

Aaron Hinni


You should be using either resource :planner either resources :planners depending on what you need. To learn about singular resource and it differences check out Rails Guides.

like image 39
Simon Perepelitsa Avatar answered Oct 12 '22 00:10

Simon Perepelitsa