0

I'm learning Rails development and am building a reddit-like app with voting on comments and posts to help learn. I have Ajax-ified the voting, and now on my local machine when I vote a post/comment up or down it sends two requests and creates two votes on either object. These votes are viewable in my votes table.

This only occurs in Firefox (not Chrome or IE), and it only occurs on my local machine (it's fine in Heroku). I verified with Firebug that it is sending two requests, but I just can't figure out why. I don't believe my asset pipeline is causing the trouble because there just isn't much in there.

This only started once I modified my voting behavior from reloading the page on each vote to using Ajax to display the vote totals. Note that a user can vote as many times as he/she wants right now

My code is on github here: https://github.com/hellomcfly/tl_postit2

My app is on Heroku here: http://fathomless-waters-1981.herokuapp.com

I don't know what code would help, but here is what is in my PostsController:

def vote
    @vote = Vote.create(voteable: @post, user: current_user, vote: params[:vote]) 

    respond_to do |format|
      format.html do
        if @vote.vote == false
          flash[:alert] = "Downvote tallied!"
          redirect_to :back
        else
          flash[:success] = "Upvote tallied!"
          redirect_to :back
        end
      end
      format.js
    end    

  end

And here is the relevant code in my posts#index template:

Net votes: <span id="post_<%=post.id%>_votes"><%= post.net_votes %></span>
<ul>
    <li><%= link_to "Vote Up", vote_post_path(post, vote: true), method: 'post', remote: true %></li>
    <li><%= link_to "Vote Down", vote_post_path(post, vote: false), method: 'post', remote: true %></li>
</ul>   

And here is the vote.js.erb code:

$("#post_<%= @post.id %>_votes").html(<%= @post.net_votes %>);

Thanks for the help.

4

1 回答 1

0

我在类似的情况下遇到了这个问题,它与在public/assets. 这导致application.js加载两次。

于 2013-04-05T00:15:29.187 回答