Question

I've added the users resource under admin to allow admin to control/modify user accounts. But I'm getting an error about uninitialized constant UsersController. User accounts are handled by devise. Currently I just want to create the index page to list each user. Here are the parts that are relevant:

/app/controllers/admin/users_controller.rb

class Admin::UsersController < ApplicationController

  def index
    @users = User.all
  end

end

I've made sure to have my devise_for :users at the start of my routes.

/conig/routes.rb

Rails.application.routes.draw do
  devise_for :users

  scope "/admin" do
    resources :users
  end

  root :to => "visitors#index"
end

I know the view isn't the problem. The page I'm trying to load displays this error.

Rails Error Output

Other than that everything else works fine. Do I need to have the UsersController handled differently? For instance do I need to inherit from a Devise controller?

Was it helpful?

Solution

scope assumes you're going to put your controllers in a namespace (so with scope it would just be UsersController not Admin::UsersController, and no /admin directory). If you want to use an Admin namespace/directory, you need to define your route like this:

  namespace :admin do
    resources :users
  end

As an aside, you can see what your routes are pointing to with rake routes. Try running it with scope vs namespace and you'll see how it changes things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top