Ruby on rails: singular resource and form_for

Unfortunately, this is a bug. You’ll have to set the url like you mention.

= form_for @order, :url => order_path do |f|

Note that this will properly route to create or update depending on whether @order is persisted.


Update

There’s another option now. You can add this to your routes config:

resolve("Order") { [:order] }

Then when the polymorphic_url method is given an object with class name “Order” it will use [:order] as the url component instead of calling model_name.route_key as described in jskol’s answer.

This has the limitation that it cannot be used within scopes or namespaces. You can route a namespaced model at the top level of the routes config:

resolve('Shop::Order') { [:shop, :order] }

But it won’t have an effect on routes with extra components, so

url_for(@order)           # resolves to shop_order_url(@order)
url_for([:admin, @order]) # resolves to admin_shop_orders_url(@order) 
                          #        note plural 'orders' ↑
                          #        also, 'shop' is from module name, not `resolve` in the second case

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)