Rails 3路由和命名空间

3
我希望有一个名为“portal”的命名空间控制器。
其中包含嵌套资源,如公司和产品。
我希望有以下路由:
/portal/:company_id/product/:id可以使用
我能够实现/portal/company/:company_id/product/:id的功能,但是想要消除URL中的'company'。
请注意,我需要存在命名空间模块portal。

使用'match'时出现了什么问题? - apneadiving
2个回答

7
我认为您可以使用scope来实现您想要的功能。可能像这样:
namespace "portal" do
  scope ":company_id" do
    resources :products
  end
end

那将会生成以下路由:
    portal_products GET    /portal/:company_id/products(.:format)          {:action=>"index", :controller=>"portal/products"}
                    POST   /portal/:company_id/products(.:format)          {:action=>"create", :controller=>"portal/products"}
 new_portal_product GET    /portal/:company_id/products/new(.:format)      {:action=>"new", :controller=>"portal/products"}
edit_portal_product GET    /portal/:company_id/products/:id/edit(.:format) {:action=>"edit", :controller=>"portal/products"}
     portal_product GET    /portal/:company_id/products/:id(.:format)      {:action=>"show", :controller=>"portal/products"}
                    PUT    /portal/:company_id/products/:id(.:format)      {:action=>"update", :controller=>"portal/products"}
                    DELETE /portal/:company_id/products/:id(.:format)      {:action=>"destroy", :controller=>"portal/products"}

编辑:不小心使用了resource而不是resources,现已更正。


0

如果您直接拼写路由,就可以将其自定义为几乎任何您想要的形式:

match '/portal/:company_id/product/:id', :to => 'companies_products#show'

:to 部分指定使用的控制器和操作,应该与您现在路由中拥有的匹配。如果您不确定是什么,请使用 rake routes 命令查看其具体解释。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接