# Instance variables in helpers?

**URL:** <https://www.ruby-forum.com/t/instance-variables-in-helpers/163474>\
**Category:** Rails\
**Created:** [March 20, 2009, 6:36pm UTC](https://www.ruby-forum.com/t/instance-variables-in-helpers/163474 "2009-03-20T18:36:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_DSoSSSSjgi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c67d28/32.png) [@Lee\_DSoSSSSjgi](https://www.ruby-forum.com/u/Lee_DSoSSSSjgi)\
**Post date:** [March 20, 2009, 6:36pm UTC](https://www.ruby-forum.com/t/instance-variables-in-helpers/163474/1 "2009-03-20T18:36:59Z")

</div>

Hi,

Can someone tell me why the following isn’t setting an instance  
variable?  
Perhaps they can’t be set/used in helpers? I have a revision\_info method  
in  
ApplicationHelper that returns the current SVN revision number (used for  
debugging info in the footer):

module ApplicationHelper  
def revision\_info  
logger.info(“In revision\_info(); rev=#{@rev}”) # @rev is always nil  
if (@rev ||= (capistrano\_revision\_info || svn\_revision\_info)).blank?  
return nil  
else  
return 'Revision ’ + @rev  
end  
end  
end

# capistrano\_revision\_info returns the revision number from the REVISION

file

# capistrano\_revision\_info returns the revision number from `svn info

–xml`

Thanks in advance for any replies.

Cheers,  
Lee

---

<div class="post-metadata">

**Author:** ![Lee\_DSoSSSSjgi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c67d28/32.png) [@Lee\_DSoSSSSjgi](https://www.ruby-forum.com/u/Lee_DSoSSSSjgi)\
**Post date:** [March 20, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/instance-variables-in-helpers/163474/2 "2009-03-20T19:32:10Z")

</div>

On 20 Mar., 18:36, Lee D’oéjgi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> if (@rev ||= (capistrano_revision_info || svn_revision_info)).blank?
> 
> ```
> 
> Thanks in advance for any replies.

At least I can tell you that it _is_ possible to set instance  
variables in helper methods _and_ get them when calling the same  
helper later on, so the problem must be somewhere else in the code.  
Did you try going to the console and check what the two methods  
#capistrano\_revision\_info and #svn\_revision\_info actually returns?  
Maybe that’s the problem, I don’t know, but it’s definitely not the  
instance variables.

–  
Cheers,  
David K.  
[http://twitter.com/rubyguy](http://twitter.com/rubyguy)

---

<div class="post-metadata">

**Author:** ![Lee\_DSoSSSSjgi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c67d28/32.png) [@Lee\_DSoSSSSjgi](https://www.ruby-forum.com/u/Lee_DSoSSSSjgi)\
**Post date:** [March 22, 2009, 8:44am UTC](https://www.ruby-forum.com/t/instance-variables-in-helpers/163474/3 "2009-03-22T08:44:04Z")

</div>

2009/3/20 removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > debugging info in the footer):  
> > end  
> > helper later on, so the problem must be somewhere else in the code.  
> > But you can only use the member variables from the class(es) which include  
> > the module.  
> > James.

---

<div class="post-metadata">

**Author:** ![Lee\_DSoSSSSjgi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c67d28/32.png) [@Lee\_DSoSSSSjgi](https://www.ruby-forum.com/u/Lee_DSoSSSSjgi)\
**Post date:** [March 22, 2009, 9:33am UTC](https://www.ruby-forum.com/t/instance-variables-in-helpers/163474/4 "2009-03-22T09:33:47Z")

</div>

maybe i misunderstand, but what i see is a helper-method in  
application\_helper. i don’t know how many times you call that method  
in one single view, but if you call it only once @rev is not yet set  
when you write it into your log file.

does the method return your revision-number correctly?

if so, why don’t you just write something like this:

Footer … \<%= revision\_info %\>
