简体   繁体   中英

rails custom controller with nested resources: routes.rb

I have a User controller and related views for index,update, etc. The project specifications changed, and now we have a custom Dashboard controller and related index page, accessed by localhost:3000/dashboard

match 'dashboard' => 'dashboard#index', as: 'dashboard'

The Dashboard index page will act similar to the (old) User index page, and so I am thinking I can simply re-use the User controller actions. How can I simply "nest" the User into Dashboard, to achieve routes like localhost:3000/dashboard/users/new or localhost:3000/dashboard/users/1/edit ? Note that the Dashboard controller does not have an associated model, it is a custom one just to create a customized homepage depending on the person viewing the Rails app. It will have other functionality unrelated to Users.

I tried

match 'dashboard' => 'dashboard#index', as: 'dashboard' do
  resources :users do

    member do
     #more custom actions
    end

    collection do
     #more custom actions
    end

 end


end 

A namespace should do it:

namespace :dashboard do
    root to: "dashboard#index"

    resources :users do
    end
end

The route for the dashboard index should be dashboard_root_path .

app
    controllers
         dashboard
             dashboard_controller.rb
             users_controller.rb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM