Ruby on Rails - add ID parameter to all routes

自闭症网瘾萝莉.ら 提交于 2019-12-21 20:08:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!