Reputation: 799
Can I get a local template var from a template in the helper?
test.html.erb:
<% my_test_var = 'wonderful' %>
<%= my_output %>
test_helper.rb:
def my_output
return @template[:my_test_var]
end
Of course @template doesn't exist. Is there a way to get it (not global with @).
Regards!
Edit:
The template test.html.erb is called a a partial render, with local variable:
render 'test', :locals => { :my_test_var = 'hallo'}
I need this passed variable in my helper.
Upvotes: 1
Views: 651
Reputation: 15634
If you want the template variables accessible in the helpers without having to pass them as arguments, the only way I know is to make them instance variables.
<% @my_test_var = 'wonderful' %>
<%= my_output %>
def my_output
return @my_test_var
end
This is not recommended though. Ideally, instance variables should be defined in controllers and used in the views. I'm not sure why you can't just pass the variable to the helper method. If that wasn't a requirement then I support krusty.ar's answer.
Upvotes: 2
Reputation: 4060
Helpers are normal methods, you can just do something like:
<% my_test_var = 'wonderful' %>
<%= my_output(my_test_var) %>
test_helper.rb:
def my_output output
return output
end
Upvotes: 0