Friday, August 22, 2008

map.resources + has_one + form_for = surprise, mofo!

If you've got a route like

map.resources :cars, :has_one => :driver


You're probably expecting to use form_for like this:

form_for([@car, @driver])


That won't work. It'll complain about not being able to find the function, 'car_drivers_path' instead of 'car_driver_path'

Fix:

form_for(@driver, :url => car_driver_path(@car))


Hopefully in Rails 2.1.1 this will be fixed using a different, less crazy syntax:

http://rails.lighthouseapp.com/projects/8994/tickets/461-fixed-polymorphic_url-to-be-able-to-handle-singleton-resources

That is all.

6 comments:

Anonymous said...

Good Post, it's help me!
Thansks!

Unknown said...

Thanks.

AntonioX said...

Works good, thanks

lucac81 said...

This seems still the case for rails 2.3.2 actually with your post your probably saved my life!!! :-) thank you

Jeff Iacono said...

Thank you for this post, seems to still be problem ... without your wise words and clearly explained solution I'd likely be up for a while longer trying to figure this out!

Anonymous said...

Thanks! Wish this was in the Rails docs...