Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1058833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:04:57+00:00 2026-05-16T18:04:57+00:00

Using Rails 3: In my update.js.erb file I found I was repeating a lot

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:04:57+00:00Added an answer on May 16, 2026 at 6:04 pm

    Add

    js.html_safe

    as last line of toggelButtonState function

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just tried to update to Rails 3.1.3 using RVM with the following commands:
Tried doing http://davidwparker.com/2008/09/17/site-wide-announcements-in-rails-using-jquery-jgrowl/ Am really bad with JS. Think I am messing up on
I'm running a Rails app and using prototype. The script breaks in IE, but
I am currently deploying a Rails application using Capistrano. Cap deploy:update_code usually works just
Using Rails v2.1, lets say you have an action for a controller that is
Using Rails 3.1.3 with Ruby 1.9.3p0. I've discovered that by default, Rails does not
Using Rails 3.1.3 and Ruby 1.9.3. I want to give the user a list
Using RAILS 2.2 Hello I am having an issue where my dates are being
Using Rails ActiveRecord, am wanting to calculate the time between two datetime fields within
When using Rails date_select with :prompt => true I see some very strange behavior

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.