Kvass
Kvass

Reputation: 8434

Rails -- routing: using a sub-directory before controller/action

Say I have an application called employeesmanager, and it has an employees resource, so /employees/new is a new employee etc. I want to have the name of the company preceding this action, so for example, ford/employees/new should create a new employee at ford, while exxon/employees/new should create a new employee at exxon. What is the best way to do this?

Ideally I don't want to have to use nested controllers with /company/exxon/employees/new because that seems like extra wasted words in the URL that can be annoying to type in.

Upvotes: 3

Views: 4087

Answers (2)

Simon Perepelitsa
Simon Perepelitsa

Reputation: 20639

scope ":company_name" do
  resources :employees
end

For more details check out Rails Guides on routing.

Upvotes: 4

KL-7
KL-7

Reputation: 47608

You can try smth like that:

get "/:company/employees/new" => "employees#new"

In that case request with GET method to this path will be passed to EmployeesController#new and in params[:company] it'll receive company name.

Upvotes: 1

Related Questions