# Magic to pass reference to a controller

**URL:** <https://www.ruby-forum.com/t/magic-to-pass-reference-to-a-controller/136048>\
**Category:** Rails\
**Created:** [April 28, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/magic-to-pass-reference-to-a-controller/136048 "2008-04-28T12:26:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [April 28, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/magic-to-pass-reference-to-a-controller/136048/1 "2008-04-28T12:26:42Z")

</div>

I’m implementing a helper library which provide both view helpers and  
controller helpers. To save the end user from having to specifically  
pass in a reference to self on every call I was wondering if there was a  
way for a class to get access to the calling controller object  
automagically.

One suggestion was Thread.current[:controller] … but that sounds a bit  
nasty

e.g.

class MyTestController \< ActionController:Base  
def test  
@ref = MyModule::MyClass.new({:a\_load\_of\_settings =\> ‘go here’})  
end  
end

module MyModule  
class MyClass  
def initialize(settings ={})  
#I need a reference to the controller  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [April 28, 2008, 2:01pm UTC](https://www.ruby-forum.com/t/magic-to-pass-reference-to-a-controller/136048/2 "2008-04-28T14:01:03Z")

</div>

Wouldn’t it be more natural to write the helper methods in modules  
that you’d mix into the controller using extend and include?

On Apr 28, 6:26 am, Alex M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
