8

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

1
  • How does app/models/admin/user.rb look like?
    – rubiii
    Commented Sep 7, 2012 at 7:41

3 Answers 3

5

If your preexisting User model isn't namespaced, try replacing

@admin_users = Admin::User.all

with

@admin_users = ::User.all
2
  • @admin_users = ::User.all gives routes error: undefined method `user_path'
    – Jawad
    Commented Sep 9, 2012 at 7:26
  • Code generated by scaffold expects your User model to be in module Admin, just like the controller. Make sure your routes.rb has something like namespace :admin { resources :users }; then go to your app/views/admin/users/index.html.erb and change link_to 'Show', admin_user to link_to 'Show', admin_user_path(admin_user); and link_to 'Destroy', admin_user, ... to link_to 'Destroy', admin_user_path(admin_user), .... Commented Sep 9, 2012 at 21:27
3

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...

3
  • there's no directory models/admin... do i have to create separate models for admin namespace, Also User.all gives routes error "undefined method `user_path'"
    – Jawad
    Commented Sep 9, 2012 at 7:24
  • If you want yes... but it is not necessary. I think in your case you don't want that. The user should be in the directory models, and then you'll always call for User.all.
    – tbem
    Commented Sep 10, 2012 at 10:43
  • gives routes error "undefined method `user_path'" this is because you don't have a users_controller but instead a admin_users_controller, so you have admin_users_path and not an users_path... and in the route you have a namespace :admin do resources :users and not a resources :users outside namespace admin
    – tbem
    Commented Sep 10, 2012 at 10:54
2

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.