9

Everytime i get a warning:

app/controllers/agency/agencies_controller.rb:1: warning: toplevel constant ApplicationController referenced by Agency::ApplicationController

My agencies_controller.rb:

class Agency::AgenciesController < Agency::ApplicationController

  def index
    ...
  end 

  ...
end

And Agency::ApplicationController:

class Agency::ApplicationController < ApplicationController
  layout 'agency'

  helper_method :current_agency
  private

  def current_agency
    @current_agency ||= current_user.agency 
  end

end

What the rails wants from me? What is the trouble?

Same situation with another controller

class Agency::ClientsController < Agency::ApplicationController
  ...
end

And no warnings, no errors...

6 Answers 6

12

I realize this question is almost two years old but I recently stumbled upon this through another stackoverflow post and wanted to share some insight.

Basically, if your namespace Agency happens to be a class instead of a module, you'll get that warning. In the stackoverflow post I pasted above, they had a model (class) of Admin and their namespace was also Admin.

This provides a better explanation of what is happening.

So check to see if your code isn't defining an Agency class somewhere. Good luck.

7

I had similar issues running Spork and Watchr in my Admin namespaced controllers. So i've fixed this by adding following code into each_run block in spec_helper.rb:

Dir[File.expand_path("app/controllers/admin/*.rb")].each do |file|
    require file
end 

All credits goes to guy from this thread

2
  • 1
    Thanks this worked great, I modified it a bit though for brevity: Dir[Rails.root.join("app/controllers/admin/*.rb")].each {|f| require f}
    – Matt Smith
    Feb 2, 2012 at 9:36
  • after hours of banging my head.. this resolved my issue.. good that i noticed that warning and good that i found this answer.. thanks a lot.
    – whizcreed
    Jul 18, 2012 at 13:15
5

ApplicationController is the name of the superclass controller that Rails generates for you when you create a new project that all your other controller classes inherit from. There's probably a conflict somewhere because you've used the same name, even though you put it within a namespace.

Try giving your Agency::ApplicationController a different name.

3
  • But why there are no troubles with Agency::ClientsController??
    – petRUShka
    Jul 7, 2010 at 11:35
  • 1
    Because ClientsController doesn't have a special meaning to Rails. Jul 7, 2010 at 12:32
  • I have a functioning Admin::ApplicationController which doesn't give problems. So renaming should not be required. There is probably ambiguity elsewhere in the call stack, which Ruby is complaining about. Jan 4, 2012 at 15:54
2

I had similar issues, after setting up Spork and Watchr. In the process, I turned off class cacheing (config_cache_classes => false in config/environments/test.rb) so that changes would be reloaded as necessary in the spork environment. Turning class cacheing back on made the warnings go away.

2

In my case it was the problem with Devise. I had a devise model Admin and a namespaced routes Admin. Changing the namespaced route to Admins solved the problem.

0

Solution for me was add this line:

# spec/rails_helper.rb
Dir[File.expand_path("app/controllers/admin/*.rb")].each { |file| require file }

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.