Ruby variable evaluation in jQuery expression is passed: onclick in Rails
I am iterating over posts and want to create toggle options for posts, but I cannot replace the div-id on posts:
<% for bill in @bills %>
<% tmp = "test"%>
<%= link_to '» now','#', :onclick => '$("#{tmp}").toggle();' %>
Instead of getting:
<a href="#" onclick="$("#test");">» now</a>
I get:
<a href="#" onclick="$("#{tmp}").toggle();">» now</a>
So there is no ruby โโvariable estimate in the row. How can i do this?
Thanks for your help and I am new to jQuery.
a source to share
For string interpolation to work in Ruby, the string must be enclosed in double quotes , not:
:onclick => '$("#{tmp}").toggle();'
use one of the following options:
-
surrounds the double quoted string and escapes literal double quotes that occur in the string:
onclick => "$(\"#{tmp}\").toggle();
-
use notation
%Q
, then you don't need to escape double quotes::onclick => %Q{$("#{tmp}").toggle();}
-
use double quotes in Ruby code and single quotes in JavaScript that will be generated:
:onclick => "$('#{tmp}'.toggle();"
a source to share