Forum: Ruby on Rails variable variable names... are you confused??

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.
7c86cccf24556c4ebe08085345c53d19?d=identicon&s=25 Roger Muthton (rogermuth)
on 2009-03-06 15:14
Hi Guys,

I got a helper method that I want to use more than once. The only thing
that will change will be the variable that is used to collect the data.
For example;

def render_blocks
    position = 0
    @variable.collect { |question, answer|
      position += 1
      "<h3>#{position}. <a
name=\"#{block_to_anchor(question)}\"></a>#{question}</h3>" +
        content_tag('div', simple_format(answer))
    }
end


I need to call this helper method from different pages. Can I pass
something through to the helper method that changes the variable name to
the one I want..

For example

<%= render_blocks :page => 'free' %> on one page that changed @variable
to @free and
<%= render_blocks :page => 'faq' %> that changes @variable to @faq.

Any help would be greatly appreciated.
9b8d00f99fe595bdc45fd30439cf9c8f?d=identicon&s=25 Franz Strebel (Guest)
on 2009-03-06 15:23
(Received via mailing list)
On Fri, Mar 6, 2009 at 3:14 PM, Roger Muthton
<rails-mailing-list@andreas-s.net> wrote:
>      position += 1
>      "<h3>#{position}. <a
> name=\"#{block_to_anchor(question)}\"></a>#{question}</h3>" +
>        content_tag('div', simple_format(answer))
>    }
> end
>
>
> I need to call this helper method from different pages. Can I pass
> something through to the helper method that changes the variable name to
> the one I want..

Just declare a parameter in your helper method à la:

def render_blocks(my_variable)
  my_variable.collect
end

You then call it in your view and pass the variable that is valid for
that view.

<%= render_blocks(@free) %>
7c86cccf24556c4ebe08085345c53d19?d=identicon&s=25 Roger Muthton (rogermuth)
on 2009-03-06 15:56
> Just declare a parameter in your helper method à la:
>
> def render_blocks(my_variable)
>   my_variable.collect
> end
>
> You then call it in your view and pass the variable that is valid for
> that view.
>
> <%= render_blocks(@free) %>

grand. cheers
This topic is locked and can not be replied to.