I encountered an error in rails trying to create a nested route in rails 2.x
map.import_time_cards 'users/:user_id/time_cards/import',
:controller => 'time_cards',
:action => 'import'
Wasn’t setting up a route for users because this route was being setup automatically and overwritten by:
map.resources :users,
:has_many => [:notes, :addresses, :expenses, :time_cards] ,
:collection => [:login, :logout, :disable, :enable]
So after digging around on the rails api I discovered that map.resources takes a block so my solution to this problem was :
map.resources(:users,
:has_many => [:notes, :addresses, :expenses] ,
:collection => [:login, :logout, :disable, :enable]) do |user|
user.resources :time_cards, :collection => [:import]
end
By using a block this tells rails to include route to ‘users/1/time_cards/import’ instead of appending import as the id for the show route.
smartlogic ::
Blog
map.resources and custom nested routes
June 5th, 2008 by Scott DavisLeave a Reply
Scott Davis's posts
- Faking a Will Paginate Collection on an Active Resource model Oct 28 2008
- Facebox With Prototype Oct 14 2008
- Making WillPaginate and Rails to_xml play nice with ActiveResource Oct 10 2008
- Advanced Model Based searches in rails Jul 21 2008
- map.resources and custom nested routes Jun 05 2008