Variable variable names... are you confused?


#1

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

#{position}. <a
name=”#{block_to_anchor(question)}">#{question}

" +
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.


#2

On Fri, Mar 6, 2009 at 3:14 PM, Roger M.
removed_email_address@domain.invalid wrote:

   position += 1
   “

#{position}. <a
name=”#{block_to_anchor(question)}">#{question}

" +
    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) %>


#3

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