How can I update this vote counter dynamically with Ajax/JavaScript (Rails)?

前端 未结 1 1176
醉话见心
醉话见心 2021-01-03 09:10

I built a simple voting system:

votes_controller.rb:

class VotesController < ApplicationController
  def vote_up
    @post = Post         


        
相关标签:
1条回答
  • 2021-01-03 09:40

    First put make a wrapper div for show.html.erb

    <div class='post-<%=@post.id%>' >
     <h3><%= @post.votes.count %> votes</h3><br />
     <%= link_to "Vote Up", vote_up_path(@post), :remote => true %>
    </div>
    

    and in vote_up.js.erb

    $("post-<%=@post.id%>").html('<%=escape_javascript @post.votes.count %>');
    

    or something like this

    0 讨论(0)
提交回复
热议问题