Rails:连接投票的jQuery问题 [英] Rails: Jquery issue with connecting to voting

查看:112
本文介绍了Rails:连接投票的jQuery问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚成功创建了一个投票系统,我想使一个按钮同时进行上下投票,而不是两个按钮.我正在为此尝试处理js文件,但是我仍然看到两个按钮,有什么建议吗?谢谢.

I just successfully created a voting system and I would like to make one button do both the voting up and down instead of two buttons. I am trying to work my way around the js file for this but I still see two buttons, any suggestions? Thank you.

Micropost Controller

class MicropostsController < ApplicationController 
  def vote_up
    @micropost = Micropost.find(params[:id])
    current_user.vote_exclusively_for(@micropost)
    respond_to do |format|
      format.html { redirect_to :back }
      format.js
      end
  end

   def unvote
    @micropost = Micropost.find(params[:id])
    current_user.vote_exclusively_against(@micropost)
    respond_to do |format|
      format.html { redirect_to :back }
      format.js
      end
  end
end

JS文件

* vote_up.js *

*vote_up.js*

$("#<%=micropost.id%>").html('<%= "#{micropost.votes_for}" %>');
$(".<%=micropost.id%>").html('<a href="/microposts/<%= micropost.id%>/unvote" data-remote="true" class="SquekCounterButton b2 <%= micropost.id%>"><span class="SquekCounterIcon <%= micropost.id%>"></span></a>');

unvote.js

$("#<%=micropost.id%>").html('<%= "#{micropost.votes_for}" %>');
$(".<%=micropost.id%>").html('<a href="/microposts/<%= micropost.id%>/vote_up" data-remote="true" class="CounterButton b2 <%= micropost.id%>"><span class="CounterIcon <%= micropost.id%>"></span></a>');

Micropost HTML

<div class='Counter'>
<span class='CounterNum'><span id='<%= micropost.id%>'><%=micropost.votes_for %></span></span>
<div class='<%=micropost.id %>'>
<a href="/microposts/<%=micropost.id %>/vote_up" data-remote='true' class='CounterButton b2'>
<span class='CounterIcon'></span>
</a>
</div>
<div class='<%=micropost.id %>'>
<a href="/microposts/<%=micropost.id %>/unvote" data-remote='true'class='CounterButton b2'>
<span class='CounterIcon'></span>
</a>
</div>
</div>

推荐答案

另一种方法:

仅创建一个动作"toggle_vote"而不是"vote_up","vote_down".第一次调用"toggle_vote"将是"vote_up",第二次调用"vote_down",等等.

Create only one action, "toggle_vote" instead "vote_up", "vote_down". First call to "toggle_vote" will "vote_up", second call "vote_down", etc..

"vote_toggle.js.erb"应根据投票状态更新按钮状态.

"vote_toggle.js.erb" should update button status depending on vote status.

这篇关于Rails:连接投票的jQuery问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆