form_form and custom parameter in path_prefix

Posted by fguillen on Stack Overflow See other posts from Stack Overflow or by fguillen
Published on 2010-05-07T16:21:23Z Indexed on 2010/05/08 9:58 UTC
Read the original article Hit count: 153

Filed under:
|
|

Hi people,

I have this route:

# config/routes.rb
map.namespace :backshop, :path_prefix => '/:shop_id/admin' do |backshop|
  backshop.resources  :items
end

And I want to use the form_for magic to reuse the same form on both: new and edit views:

<% form_for [:backshop, @item] do |f| %>

This used to works, and used to build a create url for the item or update url for the item depending on the status of the @item object.

But this is not working on this case because the routes don't exists without the shop_id parameter, and I don't know how to say to the form_for something like this:

<% form_for [:backshop, @item], :shop_id => @shop do |f| %>

Because it tries to use the @item like the :shop_id parameter.

Or like this

<% form_for [:backshop, @shop, @item] do |f| %>

Because it tries to build this url:

backshop_shop_order_path

I Know I can just to extract the form_for declaration from the partial and do different calls on depending if new or edit:

<% form_for( @item, :url => backshop_items_path( @shop ) ) do |f| %>

and

<% form_for( @item, :url => backshop_item_path( @shop, @item ) ) do |f| %>

But I just wanted don't do this because I have a bunch of models and is a few boring :)

Thanks for any suggestion

f.

© Stack Overflow or respective owner

Related posts about form-for

Related posts about parameter