toplevel constant ApplicationController referenced by Children::ApplicationController - ruby-on-rails

I am merging two applications school and children. Each working fine With children application nested in school application. Each of them have its own Database.
Here is part of the tree for school app:
app/controllers/application_controller.rb
app/controllers/user.rb
....
app/controllers/children/application_controller.rb
app/controllers/children/user.rb
....
and had these warning:
toplevel constant ApplicationController referenced by Children::ApplicationController
toplevel constant User referenced by Children::User
in app/controllers/children/application_controller.rb i have
class Children::ApplicationController < ActionController::Base
in app/controllers/application_controller.rb i have
class class ApplicationController < ActionController::Base
the nested ApplicationController is not loaded.the namespace is not working?

1-
toplevel constant ApplicationController referenced by Children::ApplicationController
I end up consolidating both applicationController in ONE applicationController ie adding content of the nested applicatioController (Children) in school applicationController to have just one applicationController.
2-
toplevel constant User referenced by Children::User
I renamed the users_controller.rb to children_users_controller.rb. And you have this first line in the controller
class Children::ChildrenUsersController < Children::ApplicationController
It was a name conflict even though they were from two different namespace! find some tips from here
hope it helps

Related

uninitialized constant Admin::AdminController

Getting an "uninitialized constant" error. I am trying to make a "AdminCotroller"(subcontroller) to the ApplicationController to control one area of the website (the "admin" area).
The routing seems to be correctly set up. I attach 2 things (both from: app/controller/admin/ folder) which produce the error:
1) the "parent" controller
class Admin::AdminController < ApplicationController
layout "admin/layout"
end
2) the "child" controller
class Admin::ProductsController < Admin::AdminController
PS: I wanted to make a separate layout and this was the only solution I could think off.
PPS: Folder Structure
Check the above file structure, that's what I have used.
And controllers as below:
class Admin::BaseController < ApplicationController
layout 'admin'
...
end
class Admin::AdminUsersController < Admin::BaseController
...
end
I don't know what I messed up those days. I haven't tried a given answer but instead I found a workable solution for naming I would like to share:
module Admin
class UsersController < BaseController
...
end
end
this way you achieve a namespace in a folder "admin" too.

uninitialized constant ApplicationController (NameError)

I changed my controller path like this.
and I have specified folder path exactly like this
class Admin::UserFoodController < Admin::ApplicationController
class Admin::UsersController < Admin::ApplicationController
class Admin::ApplicationController < ActionController::Base
but, when I restart unicorn, I got error
/home/ubuntu/hid/admin/shared/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:283:in `const_get': uninitialized constant ApplicationController (NameError)
from /home/ubuntu/hid/admin/shared/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:283:in `block in constantize'
from /home/ubuntu/hid/admin/shared/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/inflector/methods.rb:281:in `each'
so I made dummy ApplicationController in controller folder, not in admin folder
error has gone.
I don't want to make dummy ApplicationController in controller folder. I wanna keep this folder structure.
what should I do?
help me plz...
I found.
because of devise gem.
I had to change some code in devise.rb(~/2.6.4/gems/lib/devise/devise.rb)
before = > "ApplicationController"
after => "Admin::ApplicationController"
thanks all
inside app/controllers/admin/user_food_controller.rb, here is sample you can use
require_dependency "admin/application_controller"
module UserFoodController
class UserFoodController < ApplicationController
...
end
end
by using require_dependency ensures that classes are loaded and unloaded in the correct manner.
the settings above "seems similar" with setting using rails engine and here link in case you need to learn more

Uninitialized constant Admin (NameError)

I have little problem about my code I make controller/admin/moderators_controller.rb.
I got an error on compilator uninitialized constant Admin (NameError) in moderators_controller.rb.
In navigator I got this error:
superclass must be a Class (Module given)
navigator error
class Admin::ModeratorsController < ActionController
def index
end
end
This is my routes.rb file:
Rails.application.routes.draw do
namespace :admin do
resources :moderators, only: [:index]
end
end
You’ve accidentally made your controller inherit from ActionController (a module) instead of ActionController::Base (a class). You need to add ::Base to the end there.
If this is Rails 5, the common convention now is to have a ApplicationController class in your app/controllers folder, and have all controllers inherit from that (it’s just a class that inherits from ActionController::Base, but gives you a place to put common methods).
Rails 5
class Admin::ModeratorsController < ApplicationController
def index
end
end
Rails 4 or below
class Admin::ModeratorsController < ActionController::Base
def index
end
end

Rails module scope

Given the following controller structure:
# application_controller.rb
class ApplicationController < ActiveController::Base; end
# pages_controller.rb
class PagesController < ApplicationController; end
# admin/application_controller.rb
module Admin
class ApplicationController < ::ApplicationController; end
end
# admin/pages_controller.rb
module Admin
class PagesController < ApplicationController; end
end
One would expect Admin::PagesController to inherit from Admin::ApplicationController and it does. But I have noticed that sometimes it inherits from ::ApplicationController.
So I decided not to risk it and changed declaration of all controllers in /admin to specifically target Admin::ApplicationController
# admin/pages_controller.rb
module Admin
class PagesController < Admin::ApplicationController; end
end
Okay that works, but from what I know it was correct in the first place. Why Rails inherits from a wrong controller sometimes?
Admin::PagesController sometimes inherits from ApplicationController instead of Admin::ApplicationController despite both being in the same module Admin
The problem here is rails' development mode code loading: in general code is loaded when you try to do something with a constant (eg subclass from it) and that constant doesn't exist. This results in const_missing being called and rails uses it this to try to load the class (for a detailed description see the guide).
If neither ApplicationController nor Admin::ApplicationController exist then when you access your admin pages controller ruby will hit that const_missing and try to load admin/application_controller.rb
However if ApplicationController is already loaded then ruby won't fire const_missing since it perfectly legal for a class in the admin module to inherit from something at the toplevel.
The solution as you say is to make explicit what you are inheriting from. Personally in my own apps I use Admin::BaseController as the base class.
Another option is to use require_dependency to point Rails to the correct file:
# admin/application_controller.rb
require_dependency 'admin/application_controller'
module Admin
class PagesController < ApplicationController
end
end

Rspec Controllers in and out of namespace with same name

I have the following setup:
class UsersController < ApplicationController
...
end
class Admin::BaseController < ApplicationController
...
end
class Admin::UsersController < Admin::BaseController
...
end
And likewise specs:
#spec/controllers/users_controller_spec.rb:
describe UsersController do
...
end
#spec/controllers/admin/users_controller_spec.rb
describe Admin::UsersController do
...
end
All the specs run fine when run independantly, however when I run all together I get the warning:
toplevel constant UsersController referenced by Admin::UsersController
And the specs from the admin controller don't pass.
Routes file:
...
resources :users
namespace "admin" do
resources :users
end
...
Rails 4, Rspec 2.14
Can I not use the same name for controllers in different namespaces?
This happens when a top level class get autoloaded before a namespaced one is used. If you have this code without any class preloaded :
UsersController
module AdminArea
UsersController
end
The first line will trigger constant missing hook : "ok, UsersController does not exist, so let's try to load it".
But then, reaching the second line, UsersController is indeed already defined, at top level. So, there's no const_missing hook triggered, and app will try to use the known constant.
To avoid that, explicitly require proper classes on top of your spec files :
#spec/controllers/users_controller_spec.rb:
require 'users_controller'
And
#spec/controllers/admin/users_controller_spec.rb
require 'admin/users_controller'

Resources