# Warning: toplevel constant XYZ referenced Admin:XYZ

**URL:** <https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807>\
**Category:** RSpec\
**Created:** [April 15, 2011, 10:59pm UTC](https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807 "2011-04-15T22:59:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 15, 2011, 10:59pm UTC](https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807/1 "2011-04-15T22:59:57Z")

</div>

I have an odd problem. I got controllers in a namespace and  
controllers outside of the namespace. For example, I have a  
PagesController and a Admin::PagesController.

When I run rspec from the top, tests pass and I get the following  
warning:

spec/controllers/admin/pages\_controller\_spec.rb:4: warning: toplevel  
constant PagesController referenced by Admin::PagesController

This makes no sense. I do have a PagesController and an  
Admin::PagesController and specs for both that are declared properly.

I get this for 3 out of about 20 controllers.

Any ideas?

---

<div class="post-metadata">

**Author:** ![dblock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@dblock](https://www.ruby-forum.com/u/dblock)\
**Post date:** [April 15, 2011, 11:59pm UTC](https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807/2 "2011-04-15T23:59:15Z")

</div>

On Apr 15, 2011, at 11:02 AM, dblock wrote:

> This makes no sense. I do have a PagesController and an  
> Admin::PagesController and specs for both that are declared properly.
> 
> I get this for 3 out of about 20 controllers.
> 
> Any ideas?

Can you post the top 4 lines of pages\_controller\_spec?

---

<div class="post-metadata">

**Author:** ![dblock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@dblock](https://www.ruby-forum.com/u/dblock)\
**Post date:** [April 16, 2011, 1:55am UTC](https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807/3 "2011-04-16T01:55:26Z")

</div>

Hi,

On Sat, Apr 16, 2011 at 00:02, dblock [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This makes no sense. I do have a PagesController and an  
> Admin::PagesController and specs for both that are declared properly.

I would guess it’s Ruby trying to warn you that the use of the  
constant PagesController in Admin::PagesController references the  
top-level PagesController, not Admin::PagesController. It can’t know  
which one you mean.

To make the warning go away, you can use ::PagesController where you  
mean the top-level PagesController.

HTH,  
Mike

---

<div class="post-metadata">

**Author:** ![dblock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@dblock](https://www.ruby-forum.com/u/dblock)\
**Post date:** [April 17, 2011, 8:37pm UTC](https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807/4 "2011-04-17T20:37:00Z")

</div>

None of this makes any sense 🙂

dblock@dblock-magenta:~/source/Gravity/dblock$ find . -name “\*.rb” |  
xargs grep PagesController

./spec/controllers/admin/pages\_controller\_spec.rb:describe  
Admin::PagesController do  
./spec/controllers/pages\_controller\_spec.rb:describe PagesController  
do  
./app/controllers/admin/pages\_controller.rb:class  
Admin::PagesController \< AdminController  
./app/controllers/pages\_controller.rb:class PagesController \<  
ApplicationController

Now, lets compare with a controller that doesn’t yield the same issue.

dblock@dblock-magenta:~/source/Gravity/dblock$ find . -name “\*.rb” |  
xargs grep ArtworksController  
./spec/controllers/admin/artworks\_controller\_spec.rb:describe  
Admin::ArtworksController do  
./spec/controllers/artworks\_controller\_spec.rb:describe  
ArtworksController do  
./app/controllers/admin/artworks\_controller.rb:class  
Admin::ArtworksController \< AdminController  
./app/controllers/artworks\_controller.rb:class ArtworksController \<  
ApplicationController

These are identical, unless I’m blind 🙂 The only thing different is  
timestamps - it looks like in the case where things work the Admin  
spec is loaded after the non-admin spec.

---

<div class="post-metadata">

**Author:** ![dblock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@dblock](https://www.ruby-forum.com/u/dblock)\
**Post date:** [April 17, 2011, 8:37pm UTC](https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807/5 "2011-04-17T20:37:00Z")

</div>

> To make the warning go away, you can use ::PagesController where you  
> mean the top-level PagesController.

I tried saying ::PagesController in the 2 places it’s declared (the  
controller itself and the spec), no difference.

---

<div class="post-metadata">

**Author:** ![dblock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@dblock](https://www.ruby-forum.com/u/dblock)\
**Post date:** [April 17, 2011, 9:15pm UTC](https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807/6 "2011-04-17T21:15:18Z")

</div>

Top 4 lines of pages\_controller\_spec.

require ‘spec\_helper’  
require ‘spec\_controllers\_helper’

describe PagesController do  
before(:each) do  
controller.stub!(:app\_initialization).and\_return(true)  
end

Top 4 lines of admin/pages\_controller\_spec.

require ‘spec\_helper’  
require ‘spec\_controllers\_helper’

describe Admin::PagesController do  
include Devise::TestHelpers  
before(:each) do  
controller.stub!(:app\_initialization).and\_return(true)  
log\_in\_test\_user(Admin)  
end

(changing PagesController to ::PagesController does nothing)

---

<div class="post-metadata">

**Author:** ![dblock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@dblock](https://www.ruby-forum.com/u/dblock)\
**Post date:** [January 8, 2012, 1:13am UTC](https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807/7 "2012-01-08T01:13:23Z")

</div>

Someone made some sense out of this.

> **[Warning: Toplevel Constant XYZ Referenced Admin:XYZ](https://code.dblock.org/warning-toplevel-constant-xyz-referenced-adminxyz/)**
>
> I posted this to a Ruby forum a while ago.

---

<div class="post-metadata">

**Author:** ![dblock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@dblock](https://www.ruby-forum.com/u/dblock)\
**Post date:** [May 15, 2013, 4:40pm UTC](https://www.ruby-forum.com/t/warning-toplevel-constant-xyz-referenced-admin-xyz/205807/8 "2013-05-15T16:40:50Z")

</div>

the following is lifted from the ‘Gotchas’ section of my own internal  
wiki, i hope it help’s someone else some day.

Can’t load namespaced controllers

Errors:

warning: toplevel constant Finance referenced by Member::Finance

OR

uninitialized constant Finance

Cause:

a namespaced controller / helper which is listed BEFORE it’s dependency.  
e.g. ll member/finance/ bank\_batches\_controller.rb base\_controller.rb

Fix:

make this the first line of the dependent require\_dependency  
“#{RAILS\_ROOT}/app/controllers/member/finance/base\_controller”.  
requiring the dependent file before attempting to load the class. Yes,  
this goes before class …
