# Scope Confusion - Controller iVars in Modules

**URL:** <https://www.ruby-forum.com/t/scope-confusion-controller-ivars-in-modules/137905>\
**Category:** Rails\
**Created:** [May 18, 2008, 1:42am UTC](https://www.ruby-forum.com/t/scope-confusion-controller-ivars-in-modules/137905 "2008-05-18T01:42:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [May 18, 2008, 1:42am UTC](https://www.ruby-forum.com/t/scope-confusion-controller-ivars-in-modules/137905/1 "2008-05-18T01:42:09Z")

</div>

I’ve created a module to include in a Controller. Basically it works,  
but I am expecting methods in the module to have access to instance  
variables from the controller, and that appears to not be the case.  
Yet, a simple Ruby example says it should be. I’m not sure what’s  
different.

Simple Example:

module ShapeStuff  
def set\_type  
@type = ‘circle’  
end  
end

class Shape  
include ShapeStuff  
attr\_accessor :type  
def initialize  
@type = ‘square’  
end  
end

x = Shape.new  
puts x.type # ‘square’

x.set\_type  
puts x.type # ‘circle’

My distilled Rails example would be something like this. I require  
the file in application.rb.

module EventControls  
def change\_test\_var  
@test\_var = ‘y’  
end  
end

class UsersAdminController \< ApplicationController

```
 include EventControls
 layout '1col_admin'
 attr_accessor :test_var

 def some_action
   @test_var = 'x'
   change_test_var
 end

```

end

Using debug(@test\_var) the value doesn’t ever appear to change from ‘x’

Confused…

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [May 18, 2008, 1:59am UTC](https://www.ruby-forum.com/t/scope-confusion-controller-ivars-in-modules/137905/2 "2008-05-18T01:59:11Z")

</div>

On May 17, 2008, at 4:41 PM, Greg W. wrote:

> My distilled Rails example would be something like this. I require  
> the file in application.rb

oops, I meant environment.rb, of course

and… just to add more to this, I expected session to be available  
to the module methods too, but it isn’t.

for now I’m passing ivars and session as params to module methods,  
but I’d really like to get rid of that.

– gw
