0
按鈕我有一個表決機制,你可以投票向上/向下在給定的比賽中的條目。投票數量有限,每個條目旁邊都有一個向上和向下的按鈕。如果沒有剩餘選票,我想向上按鈕更改樣式,如果用戶還沒有投票,可以使用向下按鈕更改樣式。Ruby on Rails的 - 無法jQuery來更新點擊
在我_entry.html我:
<div class="vote-box">
<% if !current_user.voted?(entry) %>
<%= link_to '-', entry_vote_down_path(entry), method: :post, remote: true, :class => "btn btn-success vote down disabled" %>
<% else %>
<%= link_to '-', entry_vote_down_path(entry), method: :post, remote: true, :class => "btn btn-success vote down" %>
<% end %>
<% if current_user.votes_remaining(entry.contest) == 0 %>
<%= link_to '+', entry_vote_up_path(entry), method: :post, remote: true, :class => "btn btn-success vote up disabled" %>
<% else %>
<%= link_to '+', entry_vote_up_path(entry), method: :post, remote: true, :class => "btn btn-success vote up" %>
<% end %>
<p><%= pluralize entry.votes.count, 'total vote' %> </p>
的entry_vote_up_path,例如,最終觸發vote_up在EntriesController:
def vote_up
entry = Entry.find(params[:entry_id])
current_user.vote_up!(entry)
flash[:notice] = "Vote successfully counted."
respond_to do |f|
f.js { @entry = entry }
end
end
然後我有一個vote_up.coffee :
<% if current_user.votes_remaining(@entry.contest) == 0 %>
$('#entry_<%= @entry.id %> .vote-box a.vote_up').replaceWith('<%= vote_up_button_for_entry_disabled(@entry) %>')
<% else %>
$('#entry_<%= @entry.id %> .vote-box a.vote_up').replaceWith('<%= vote_up_button_for_entry(@entry) %>')
的vote_up_button_for_entry不一樣的原始HTML文件中的link_to,我知道這工作。我覺得問題與「a.vote_up」有關,但我無法弄清楚。謝謝!