map.resources and custom nested routes

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.

Author image

About Scott Davis

You've successfully subscribed to SmartLogic Blog
Great! Next, complete checkout for full access to SmartLogic Blog
Welcome back! You've successfully signed in.
Unable to sign you in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.