0

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:

 $(\&quot;#show_today_check_2\&quot;).removeClass(\&quot;orange\&quot;).addClass(\&quot;gray\&quot;).attr(\&quot;value\&quot;,\&quot;1\&quot;);

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 \&quot;s?

1 Answer 1

2

Add

js.html_safe

as last line of toggelButtonState function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.