robzolkos
robzolkos

Reputation: 2266

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.

Upvotes: 3

Views: 779

Answers (2)

DanneManne
DanneManne

Reputation: 21180

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.

Upvotes: 7

tadman
tadman

Reputation: 211540

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.

Upvotes: 0

Related Questions