Form_form and custom parameter in path_prefix

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 is used to work and is used to generate a create url for an item or an update url for an item depending on the @item state .

But that doesn't work in this case, because the routes don't exist without the shop_id parameter , and I don't know how to tell form_for something like:

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

      

As it tries to use @item as parameter : shop_id .

Or like this

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

      

Because it's trying to build this url:

backshop_shop_order_path

      

I know I can just extract the form_for declaration from the partial and make different calls depending on whether it's 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 not to, because I have a bunch of models and a little boring :)

Thanks for any suggestion

e.

+2


a source to share


1 answer


It 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 can use [@shop, @item]

to get the route you want. Use rake routes

to check routes and see if you like them.

0


a source







All Articles