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 8345219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:36:45+00:00 2026-06-09T06:36:45+00:00

I am creating an upvote/downvote functionality on an app I am building. When a

  • 0

I am creating an upvote/downvote functionality on an app I am building. When a user hits the upvote or downvote button, an ajax request is submitted to a controller which updates a vote count in the database and then runs a javascript file that uses jquery to update the displayed votes count and disable the button pressed. Here is the jquery to update the display:

$('#post-action-<%= "#{@post.id}" %>').html("
                <i class=\"icon-arrow-up arrow-voted\"></i>
                <p class=\"votes_difference\" ><%= (post.upvotes - post.downvotes) %></p>
            <%= link_to raw(\"<i class=\"icon-arrow-down\"></i>\"), post_downvote_path(post.id), method: :put, remote: true %>
                <p class=\"votes_sum\" ><%= (post.upvotes + post.downvotes) %> votes</p>
");

Everything works, except for updating the vote display and changing the upvote button (in other words, the above code is not being run. The database is updated, so I know the conroller is run successfully.). I also know that the problem is with the jquery above becuase it was working when I was rendering a much less html. Here is the simplier code that I previously had working:

$('#post-action-<%= "#{@post.id}" %>').html("<%= @post.upvotes - @post.downvotes %>")

I am postive that I am grabbing the correct element. The html element content I am replacing is:

<div class="post_actions" id="post-action-<%= "#{post.id}" %>" >

...some embedded ruby and other html

</div>

I assuming there is some jquery syntax error, but I simply cannot find it. I am at my wits end here. Any and all input on this matter would be appreciated.

EDIT

As referenced in the comments of Trip’s answer, I was able to print foobar by changing this:

$('#post-action-<%= "#{ @post.id }" %>').html("fooooooobar!")

to this:

$('#post-action-<%= @post.id %>').html("fooooooobar!")

However, when I then replace foobar with the more complex code:

$('#post-action-<%= @post.id %>').html("

                <i class='icon-arrow-up arrow-voted'></i>
                <p class='votes_difference' >
                  <%= (@post.upvotes - @post.downvotes) %>
                </p>
            <%= link_to raw('<i class=\"icon-arrow-down\"></i>'), post_downvote_path(@post.id), method: :put, remote: true %>
                <p class='votes_sum' ><%= (@post.upvotes + @post.downvotes) %> votes</p>

");

The problem returns. The user display does not update. However, this time around, the console has a jquery error:

PUT http://localhost:3000/course/1/upvote_post 500 (Internal Server Error) jquery.js:8241
jQuery.ajaxTransport.send jquery.js:8241
jQuery.extend.ajax jquery.js:7720
$.rails.rails.ajax jquery_ujs.js:99
$.rails.rails.handleRemote jquery_ujs.js:158
(anonymous function) jquery_ujs.js:309
jQuery.event.dispatch jquery.js:3333
jQuery.event.add.elemData.handle.eventHandle

EDIT 2

After playing around with the jquery code, I have noticed a couple of things. Firstly, I have to minify the code. If I do not, the jQuery does not fire. For example, I should do this:

$('#post-action-<%= "#{ @post.id }" %>').html("fooooooobar!")

instead of this:

$('#post-action-<%= "#{ @post.id }" %>').html("
              fooooooobar!
")

Secondly, after entering each line back into the html method, I have identified the line that causes a problem. This line:

<%= link_to raw(\"<i class=\"icon-arrow-down\"></i>\"), post_downvote_path(post.id), method: :put, remote: true %>

Causes the jquery to fail. I tried the line without the ‘raw’:

 <%= link_to "<i class='icon-arrow-down'></i>", post_downvote_path(post.id), method: :put, remote: true %>

But still nothing.

SOLUTION – AH YEA

In order to get the link line to work, I had to escape it like so:

<%= escape_javascript(link_to raw("<i class='icon-arrow-down'></i>"), post_downvote_path(@post.id), method: :put, remote: true) %>

Finally! Free at last.

  • 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-06-09T06:36:46+00:00Added an answer on June 9, 2026 at 6:36 am

    I think it’s this line :

    <%= link_to raw(\"<i class=\"icon-arrow-down\"></i>\"),
    

    So do this :

    $('#post-action-<%= "#{@post.id}" %>').html("
                    <i class='icon-arrow-up arrow-voted'></i>
                    <p class='votes_difference' >
                      <%= (post.upvotes - post.downvotes) %>
                    </p>
                <%= link_to raw('<i class=\"icon-arrow-down\"></i>'), post_downvote_path(post.id), method: :put, remote: true %>
                    <p class='votes_sum' ><%= (post.upvotes + post.downvotes) %> votes</p>
    ");
    

    Update

    The reason this line fails :

    \"<i class=\"icon-arrow-down\"></i>\"),
    

    Is because you are using /" to enclose another /" so the app is thinking that there are two parts here :

    \"<i class=\"
    

    and

    \"></i>\"
    

    You need to differentiate them. You are already using " for your html() method so use '

    So it would look like this :

    '<i class=\"icon-arrow-down\"></i>'),
    

    Secondly, escape_javascript the link:

    <%= escape_javascript(link_to raw("<i class='icon-arrow-down'></i>"), post_downvote_path(@post.id), method: :put, remote: true) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

creating a very simple scheduling app I am asking the user to tell me
I am creating a site where anyone is able to upvote and downvote content.
I'm creating a simple upvote/downvote system similar to reddit's, where users can upvote/downvote something
Creating an a app, where I store bunch of photos in the drawable folder,
Creating a google map with store locations within 50 miles of user entered address.
Creating a JApplet I have 2 Text Fields, a button and a Text Area.
Creating a calculator-like dialog, I noticed that quickly clicking on a button in IE
Creating a Certificate authority signing request (Keychain Access, Certificate Assistant: Request Certificate from Certificate
Creating a searchable contacts kind of app. Below is my code public class EmployeeList
Creating this ajax styled wordpress portfolio theme and I'm a little stuck on how

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.