I'm trying to figure out how to nest routes in Rails 5 (so that related controllers are kept together.
I have my controllers file tree set up as:
app/controllers/users
In that folder, I have controllers for:
identities_controller.rb
app_roles_controller.rb
Each of those controllers is saved as:
class Users::IdentitiesController < ApplicationController
class Users::AppRolesController < ApplicationController
My routes file has:
resources :app_roles,
:controllers => {
:app_roles => 'users/app_roles'
}
devise_for :users,
:controllers => {
:sessions => 'users/sessions',
:registrations => "users/registrations",
:omniauth_callbacks => 'users/omniauth_callbacks'
}
resources :identities,
:controllers => {
:identities => 'users/identities'
}
resources :users
In my views folder, all of the files are top-level. Im unclear as to whether I need to group them in the same way that I do my controllers.
When I save all of this and try to navigate to http://localhost:3000/app_roles#index, I expect to go to my app/views/app_roles/index.
Instead, I get an error that says:
app_roles
uninitialized constant AppRolesController
When I rake routes, I get:
rake routes | grep app_roles
app_roles GET /app_roles(.:format) app_roles#index {:controllers=>{:app_roles=>"users/app_roles"}}
POST /app_roles(.:format) app_roles#create {:controllers=>{:app_roles=>"users/app_roles"}}
new_app_role GET /app_roles/new(.:format) app_roles#new {:controllers=>{:app_roles=>"users/app_roles"}}
edit_app_role GET /app_roles/:id/edit(.:format) app_roles#edit {:controllers=>{:app_roles=>"users/app_roles"}}
app_role GET /app_roles/:id(.:format) app_roles#show {:controllers=>{:app_roles=>"users/app_roles"}}
PATCH /app_roles/:id(.:format) app_roles#update {:controllers=>{:app_roles=>"users/app_roles"}}
PUT /app_roles/:id(.:format) app_roles#update {:controllers=>{:app_roles=>"users/app_roles"}}
DELETE /app_roles/:id(.:format) app_roles#destroy {:controllers=>{:app_roles=>"users/app_roles"}}
To me, I think these routes show that app_roles#index should go to the app/views/app_roles/index.html.erb via the controller in app/controllers/users/app_roles_controller.rb
I have the same issue with the identities resource.
GUESSES I tried moving the app/views/app_roles folder to be nested under the users folder (i.e. app/views/users), but I get the same error when I then try to go to the http://localhost:3000/app_roles#index to check if it works.
I also tried amending the routes file to:
resources :app_roles,
:resources => {
:app_roles => 'users/app_roles'
}
By that, I mean that I changed the reference to :controllers, to :resources. It didn't work - I get the same error.
Can anyone see what I'm doing wrong?