Using Rails 3: In my update.js.erb file I found I was repeating a lot of stuff. So I tried to put it all into a helper. But I'm having trouble getting the helper to give back clean javascript. It puts in \"
everywhere instead of "
Here's what I started with:
<% if @list.show_today %>
$("#show_today_check_<%= @list.id %>").removeClass("gray").addClass("orange").attr("value","0");
<% else %>
$("#show_today_check_<%= @list.id %>").removeClass("orange").addClass("gray").attr("value","1");
<% end %>
<% if @list.show_inventory %>
$("#show_inventory_check_<%= @list.id %>").removeClass("gray").addClass("white").attr("value","0");
<% else %>
$("#show_inventory_check_<%= @list.id %>").removeClass("white").addClass("gray").attr("value","1");
<% end %>
etc.
Here's the helper I wrote to generate the above javascript:
def toggelButtonState( object, name, color)
if object.send(name)
@add_col = color
@rem_col = 'gray'
@value = "0"
else
@add_col = 'gray'
@rem_col = color
@value = "1"
end
js = '$("#'
js += "#{name}_check_#{@list.id}"
js += '").removeClass("'
js += @rem_col
js += '").addClass("'
js += @add_col
js += '").attr("value","'
js += @value
js += '");'
end
I call it with:
<%= toggelButtonState( @list , 'show_today', 'orange' ) %>
And here's what I get in the response:
$(\"#show_today_check_2\").removeClass(\"orange\").addClass(\"gray\").attr(\"value\",\"1\");
Now I did notice a similar problem with straight html in helpers. It wouldn't let me return stuff in angle brackets. But then I found out about content_tag. Is there something similar for javascript? How can I get rid of the \"
s?