Reputation: 193
I'm making admin panel in my app, I made the scaffold user controller for admin (User Model already exists) like this:
rails g scaffold_controller Admin::User username:string password_digest:string role:string
and in routes
namespace :admin do
resources :users
resources :dashboard
end
and controllers/admin/users_controllers.erb looks like
class Admin::UsersController < ApplicationController
# GET /admin/users
# GET /admin/users.json
def index
@admin_users = Admin::User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @admin_users }
end
end
so when i go to url /admin/users i got the following error:
NameError in Admin::UsersController#index
uninitialized constant Admin::User
How do i solve this problem
Thanks
Upvotes: 8
Views: 10325
Reputation: 22224
In my particular case, I had named the files and classes correctly but the containing folder was named incorrectly.
I had:
/models/maps/type.rb
I had to change it to:
/models/map/type.rb
Notice the singular folder name. Changing it to singular allowed Rails to automatically load the right class and no longer have this error at runtime.
Upvotes: 2
Reputation: 605
I think the generator doesn't created the directory models/admin so you should call User.all and not Admin::User.all.
Check if the user.rb is in models or models/admin...
Upvotes: 3
Reputation: 3462
If your preexisting User
model isn't namespaced, try replacing
@admin_users = Admin::User.all
with
@admin_users = ::User.all
Upvotes: 5