views:

81

answers:

1

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.

A: 

Looks like you want to do nested resources with a namespace maybe you can rewrite your routes to something like

map.namespace(:admin) do |admin|
  admin.resources :shops do |admin_shop|
     admin_shop.resources :item
  end
end

Now you should be able to use [@shop, @item] to get the route you want. Use rake routes to examine the routes and see if they are to your liking.

Corey
Thanks Corey, this is very helpful.
fguillen