Rails namespace uninitialized constant

I create an admin panel in my application, I created a user forest controller for the administrator (the user model already exists) as follows:

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 the / admin / users _controllers.erb controllers look 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 to solve this problem

thanks

+6
source share
3 answers

If your existing User model does not fit in the namespace, try replacing

 @admin_users = Admin::User.all 

from

 @admin_users = ::User.all 
+5
source

I think the generator did not create the / admin directory model, so you should call User.all, not Admin :: User.all.

Check if user.rb is in / admin models ...

+3
source

In my particular case, I correctly named the files and classes, but the containing folder was named incorrectly .

I have had:

 /models/maps/type.rb 

I had to change it to:

 /models/map/type.rb 

Pay attention to the unique name of the folder. Changing it to singular, Rails automatically loads the correct class and no longer has this error at runtime.

0
source

Source: https://habr.com/ru/post/924825/


All Articles