0

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 

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

I followed your suggestion, and re-arranged the location of the controllers and view (views/dashboard/user/*). The issue is that rails is complaining that users_path is undefined for #<#<Class:0x0000012afccdf0>:0x0000012af97cb8>. This happens when I try to use the form to create a new user. The offending line is <%= simple_form_for(@user) do |f| %>
views/dashboard/user/* probably a typo, but user should be plural. You need to change your form to simple_form_for([:dashboard, @user]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.