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

petRUShka
  • 9,812
  • 12
  • 61
  • 95

6 Answers6

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.

Community
  • 1
  • 1
Bart Jedrocha
  • 11,450
  • 5
  • 43
  • 53
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

Vadim Chumel
  • 165
  • 1
  • 5
  • 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 02 '12 at 09: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 '12 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.

John Topley
  • 113,588
  • 46
  • 195
  • 237
  • But why there are no troubles with Agency::ClientsController?? – petRUShka Jul 07 '10 at 11:35
  • 1
    Because `ClientsController` doesn't have a special meaning to Rails. – John Topley Jul 07 '10 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. – Joost Baaij Jan 04 '12 at 15:54
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.

tundrax
  • 21
  • 1
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.

Yardboy
  • 2,777
  • 1
  • 23
  • 29
0

Solution for me was add this line:

# spec/rails_helper.rb
Dir[File.expand_path("app/controllers/admin/*.rb")].each { |file| require file }
Štefan Bartoš
  • 561
  • 6
  • 8