我是新來的紅寶石,我只是玩了一個演示應用程序。我已經包括了gem acts_as_votable,除了一件事情我缺少它之外,它工作得很好... ajax。 刷新所有的時間頁面它有點乏味,所以我已經「嘗試」實現ajax,但沒有運氣:(。也許兩個小時的資源後,我想我什麼也不能做,我需要幫助實現ajax 。上向下和向上的投票非常感謝Act Votable Ajax Fail
這裏是我的代碼:
entries_controller.rb
def upvote
@entry = Entry.find(params[:id])
unless current_user.voted_for? @entry
@entry.vote_total = @entry.vote_total + 1
@entry.save
@entry.upvote_by current_user
else
flash[:danger] = 'Sorry!! You had allready voted this entry!'
end
redirect_to :back
end
def downvote
@entry = Entry.find(params[:id])
unless current_user.voted_for? @entry
@entry.vote_total = @entry.vote_total + 1
@entry.save
@entry.downvote_by current_user
else
flash[:danger] = 'Sorry!! You had allready voted this entry!'
end
redirect_to :back
end
_entry.html.erb(在項目文件夾)
<aside class="vote-count bind-<%= entry.id %>">
<%= link_to like_entry_path(entry), :remote => true, method: :put, class: 'vpos' do %>
<i class="fa fa-thumbs-up"></i>
<%= entry.get_upvotes.size %>
<% end %>
<%= link_to dislike_entry_path(entry), :remote => true, method: :put, class: 'npos' do %>
<i class="fa fa-thumbs-down"></i>
<%= entry.get_downvotes.size %>
<% end %>
</aside>
upvote.js.erb(在項目文件夾)
$(".bind-<%=entry.id%>").html('<%=escape_javascript entry.get_upvotes.size %>');
的routes.rb
resources :entries, only: [:index, :show, :new, :create, :destroy] do
member do
put 'like', to: 'entries#upvote'
put 'dislike', to: 'entries#downvote'
end
end
我是一個有點新的軌道的幫助,所以anykind。將不勝感激! 謝謝
感謝您的支持!超級有幫助的。我有一個關於你的js.erb文件的問題 - 這些功能是什麼,你把它們放在哪裏?不Rails':remote =>真正處理AJAX請求? – 2015-04-06 19:32:48