# What is @controller instance variable?

**URL:** <https://discuss.rubyonrails.org/t/what-is-controller-instance-variable/53524>\
**Category:** rubyonrails-talk\
**Created:** [August 9, 2010, 7:14pm UTC](https://discuss.rubyonrails.org/t/what-is-controller-instance-variable/53524 "2010-08-09T19:14:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 9, 2010, 7:14pm UTC](https://discuss.rubyonrails.org/t/what-is-controller-instance-variable/53524/1 "2010-08-09T19:14:46Z")

</div>

In a book I'm reading the author tries to do the following from within his controller test file:

class PagesControllerTest \< ActionController::TestCase &nbsp;&nbsp;def test\_create\_link\_in\_index &nbsp;&nbsp;&nbsp;&nbsp;get :index &nbsp;&nbsp;&nbsp;&nbsp;assert\_select "a[href=?]", url\_for(:action =\> :new), :text =\> 'Neue Seite' &nbsp;&nbsp;end end

However the method 'url\_for' is unknown within the class PagesControllerTest, as it is defined in ActionController::Base.

To get around this he writes (although this is not the final solution):

assert\_select "a[href=?]", @controller.url\_for(:action =\> :new, :only\_path =\> true), :text =\> 'Neue Seite'

How is he able to reference a method defined in another class, simply by prefixing the method call with '@controller'

Could someone explain what is '@controller' in this context?

I tried Googling it, but as Google ignores the '@' I couldn't get very far.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 9, 2010, 7:29pm UTC](https://discuss.rubyonrails.org/t/what-is-controller-instance-variable/53524/2 "2010-08-09T19:29:12Z")

</div>

it's an instance of the controller class that you're testing (ActionController::TestCase sets one up for you)

Fred

---

<div class="post-metadata">

**Author:** ![Bill\_Walton1](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Bill\_Walton1](https://discuss.rubyonrails.org/u/Bill_Walton1)\
**Post date:** [August 9, 2010, 7:33pm UTC](https://discuss.rubyonrails.org/t/what-is-controller-instance-variable/53524/3 "2010-08-09T19:33:30Z")

</div>

Hi Jim,

---

<div class="post-metadata">

**Author:** ![Chris\_Mear2](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@Chris\_Mear2](https://discuss.rubyonrails.org/u/Chris_Mear2)\
**Post date:** [August 9, 2010, 7:33pm UTC](https://discuss.rubyonrails.org/t/what-is-controller-instance-variable/53524/4 "2010-08-09T19:33:53Z")

</div>

'@controller' is an instance variable in ActionController::TestCase. ActionController::TestCase sets up this variable to contain an instance of the controller class you are testing (in this case, PagesController).

In other words, ActionController::TestCase does something like this:

@controller = PagesController.new

which is why you're able to call PagesController methods on @controller.

(The '@' symbol before a variable in Ruby just means that it is an instance variable, as opposed to an ordinary local variable.)

Chris

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 9, 2010, 8:08pm UTC](https://discuss.rubyonrails.org/t/what-is-controller-instance-variable/53524/5 "2010-08-09T20:08:33Z")

</div>

Hi,

Thanks for all of the replies. That has really made things a lot clearer for me.

So, to summarize:

'url\_for' is a method defined in ActionController::Base.

@controller is an instance variable initiated by ActionController::TestCase containing a reference to my controller (PagesController).

PagesController inherits from ApplicationController.

ApplicationController inherits from ActionController::Base.

Therefore, in ActionController::TestCase, by prepending a method call with @controller, owing to the aforementioned class hierarchy, I can access methods defined in ActionController::Base.

Would that be correct?

Thanks again for your help.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 9, 2010, 8:44pm UTC](https://discuss.rubyonrails.org/t/what-is-controller-instance-variable/53524/6 "2010-08-09T20:44:51Z")

</div>

> Hi,
> 
> Thanks for all of the replies. That has really made things a lot clearer for me.
> 
> So, to summarize:
> 
> 'url\_for' is a method defined in ActionController::Base.
> 
> @controller is an instance variable initiated by ActionController::TestCase containing a reference to my controller (PagesController).
> 
> PagesController inherits from ApplicationController.
> 
> ApplicationController inherits from ActionController::Base.
> 
> Therefore, in ActionController::TestCase, by prepending a method call with @controller, owing to the aforementioned class hierarchy, I can access methods defined in ActionController::Base.

Some of the way you phrased that seems a bit awkward to me. It not that @controller contains a reference to you controller - it is an actual instance of it. And 'prepending a method call' sounds like you think there is some magic going on - there isn't - methods are always called on a certain object (omitting the object means the method is called on self)

Fred

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 9, 2010, 9:39pm UTC](https://discuss.rubyonrails.org/t/what-is-controller-instance-variable/53524/7 "2010-08-09T21:39:35Z")

</div>

> It not that @controller contains a reference to you controller - it is an actual instance of it.

OK, I phrased that badly.

> And 'prepending a method call' sounds like you think there is some magic going on - there isn't - methods are always called on a certain object (omitting the object means the method is called on self)

And that too 🙂Thanks for clearing that up.
