Forum: Ruby on Rails Instance variables in helpers?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cc00f3475371b535360056f165a7caf8?d=identicon&s=25 Lee D'oéjgi (Guest)
on 2009-03-20 18:36
(Received via mailing list)
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
36ed63ef68c172f148c491418395d0b5?d=identicon&s=25 rubyguy@ymail.com (Guest)
on 2009-03-20 19:32
(Received via mailing list)
On 20 Mar., 18:36, Lee D'oéjgi <leedoe...@gmail.com> 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 Knorr
http://twitter.com/rubyguy
D2536dbfa799820f207aaa74832a26d5?d=identicon&s=25 James Cowlishaw (Guest)
on 2009-03-22 08:44
(Received via mailing list)
2009/3/20 rubyguy@ymail.com <rubyguy@ymail.com>

> > 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.
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-03-22 09:33
(Received via mailing list)
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 %>
This topic is locked and can not be replied to.