开发者

Rails 3 routing and namespaces

I want to have a namespaced controller called 'portal'.

in this will be nested resources such as companies and products.

开发者_开发技巧

I'd like routes like :

/portal/:company_id/product/:id to work

I can get

/portal/company/:company_id/product/:id to work but would like to eliminate the 'company' in the url

Hope that is clear. Please keep in mind that I need the namespaced module portal to exist.


I think you could use scope to achieve what you want. Perhaps like this:

namespace "portal" do
  scope ":company_id" do
    resources :products
  end
end

That will generate the following routes:

    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"}

Edit: Accidentally used resource instead of resources. Fixed now.


You can customize the routes to nearly whatever you want if you spell them out directly, like this:

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

The :to part specifies the controller and action to use, something that should match what you have in your routes now. If you're not sure what that is, rake routes will tell you its specific interpretation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜