Ruby-on-rails – Rails 3 Roulette and Name Space

I want a namespace controller named “portal”.

This will be nested resources, such as companies and products.< /p>

I want a route like the following:

/portal/: company_id / product/: id work

I can get it

/portal / company /: company_id / product /: id works but want to eliminate the’company’ in the url

I hope it’s clear. Please remember that I need to have a namespace module portal.

div>

I think you can use ranges to achieve what you want. Maybe something like this:

< p>

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

This will Generate the following route:

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: unexpected use of resources instead of resources. Now Fixed.

I want a namespace controller named “portal”.

This will be a nested resource , Such as company and product.

I want a route like the following:

/portal/: company_id / product/: id work

I can get

p>

/portal / company /: company_id / product /: id works but want to eliminate the’company’ in the url

I hope it’s clear. Please remember that I need to exist in the namespace module portal .

I think you can use ranges to achieve what you want. Maybe it’s like this:

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

This will produce the following route:< /p>

portal_products GET /portal/:company_id/products(.:format) {:actio n=>"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 resources instead of resources. Now fixed.

Leave a Comment

Your email address will not be published.