开发者

form_form and custom parameter in path_prefix

开发者 https://www.devze.com 2022-12-30 01:31 出处:网络
I have this route: # config/routes.rb map.namespace :backshop, :path_prefix => \'/:shop_id/admin\' do |backshop|

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开发者_Python百科 just wanted don't do this because I have a bunch of models and is a few boring :)

Thanks for any suggestion

f.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消