问题
I am wondering what is the best way to implement the following functionality in the Rails routing:
Scenario: an website where users sign up for accounts => accountID (Account) becomes the main entity within the website.
Example: https://basecamp.com/:ID/ - takes the authorized users to the Basecamp dashboard. From here all the URLs contain the :accountID as in https://basecamp.com/:ID/projects - list all the projects under the account.
Many thanks in advance!
回答1:
Use route prefixing:
scope ":account_id" do
resources :projects
...
end
This will always give you params[:account_id] on each resource controller that is defined within the scope.
Read more here: http://guides.rubyonrails.org/routing.html#prefixing-the-named-route-helpers
Update: Here's an entire example including your "dashboard" default route
scope ":account_id" do
root :to => "dashboard#index" # http://example.com/12323/
resources :projects # http://example.com/12323/projects
resources :todos # http://example.com/12323/todos
...
end
来源:https://stackoverflow.com/questions/14867314/ruby-on-rails-add-id-parameter-to-all-routes