2014-12-04 85 views
6

我是新來的紅寶石,我只是玩了一個演示應用程序。我已經包括了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。將不勝感激! 謝謝

回答

6

Finnaly我已經設法使它工作(作爲投票寶石,票現在ajax)。我要分享它,因爲有人和我有同樣的問題。

routes.rb中:

put 'like', to: 'entries#upvote' 
put 'dislike', to: 'entries#downvote' 

entries_controller.rb(在我的情況下):

def upvote 
    @entry = Entry.find(params[:id]) 
    respond_to do |format| 
    unless current_user.voted_for? @entry 
    format.html { redirect_to :back } 
    format.json { head :no_content } 
    format.js { render :layout => false } 
    @entry.vote_total = @entry.vote_total + 1 
    @entry.save 
    @entry.upvote_by current_user 
    else 
    flash[:danger] = 'You allready voted this entry' 
    format.html { redirect_to :back } 
    format.json { head :no_content } 
    format.js 
    end 
end 
end 

def downvote 
    @entry = Entry.find(params[:id]) 
    respond_to do |format| 
    unless current_user.voted_for? @entry 
    format.html { redirect_to :back } 
    format.json { head :no_content } 
    format.js { render :layout => false } 
    @entry.vote_total = @entry.vote_total + 1 
    @entry.save 
    @entry.downvote_by current_user 
    else 
    flash[:danger] = 'You allready voted this entry' 
    format.html { redirect_to :back } 
    format.json { head :no_content } 
    format.js 
    end 
end 
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> 
    <span class="vcount-<%= entry.id %>"><%= entry.get_upvotes.size %></span> 
<% end %> 
<%= link_to dislike_entry_path(entry), :remote => true, method: :put, class: 'npos' do %> 
    <i class="fa fa-thumbs-down"></i> 
    <span class="ncount-<%= entry.id %>"><%= entry.get_downvotes.size %></span> 
<% end %> 
</aside> 

upvote.js .erb:

$('.vpos').bind('ajax:success', function() { 
$('.vcount-<%= @entry.id %>').html('<%=escape_javascript @entry.get_upvotes.size.to_s %>'); 
}); 

downvote.js.erb:

$('.npos').bind('ajax:success', function() { 
$('.ncount-<%= @entry.id %>').html('<%=escape_javascript @entry.get_downvotes.size.to_s %>'); 
}); 

這就是全部。希望我能幫助需要幫助的人。 乾杯。

+0

感謝您的支持!超級有幫助的。我有一個關於你的js.erb文件的問題 - 這些功能是什麼,你把它們放在哪裏?不Rails':remote =>真正處理AJAX請求? – 2015-04-06 19:32:48

1

這是我的解決方案。 該項目:用戶可以創建一個帖子(辯論)和其他用戶可以upvote和downvote與按鈕。

所不同的是:

阿賈克斯刷新downvoteS計數和upvotes在同一時間,當用戶的點擊次數。 我在.jtml文件中使用.text代替.html。 我沒有使用partial,我把所有的代碼都放在了debat.html.erb中。 我創建特定類,跨度和可變標識每個按鈕,因爲所有的按鈕在同一時間

我的routes.rb是一樣的改變:

resources :debats do 
member do 
    put "like", to: "debats#upvote" 
    put "dislike", to: "debats#downvote" 
end 
end 

dabats_controller。RB:

def upvote 
    @debat = Debat.find(params[:id]) 
    @debat.upvote_by current_user 
    respond_to do |format| 
     format.html {redirect_to :back } 
     format.json { render json: { count: @debat.liked_count } } 
     format.js { render :layout => false } 
    end 
end 

def downvote 
    @debat = Debat.find(params[:id]) 
    @debat.downvote_by current_user 
    respond_to do |format| 
    format.html {redirect_to :back } 
    format.json { render json: { count: @debat.disliked_count } } 
    format.js { render :layout => false } 
end 
end 

debat.html.erb(僅與2個按鈕的部分)

<div class="large-1 columns count"> 
    <span class= "like-<%= debat.id %>"> 
     <%= link_to like_debat_path(debat), method: :put, remote: true, class: 'like' do %> 
     <div class="upcount"><i class="fa fa-angle-up"></i></div> 
     <div class="ouicount up-<%= debat.id %>"><%= debat.get_upvotes.size %></div> 
     <div class="oui">Oui</div> 
     <%end%> 
    </span> 

    <span class= "dislike-<%= debat.id %>"> 
     <%= link_to dislike_debat_path(debat), method: :put, remote: true, class: 'dislike' do %> 
     <div class="non">Non</div> 
     <div class="noncount down-<%= debat.id%>"> <%= debat.get_downvotes.size %></div> 
     <div class="downcount"><i class="fa fa-angle-down"></i></div> 
     <% end %> 
    </span> 
    </div> 

upvote.js.erb

$('.like').bind('ajax:success', function() { 

    $('.up-<%= @debat.id %>').text(<%= @debat.get_upvotes.size%>); 
    $('.down-<%= @debat.id %>').text(<%= @debat.get_downvotes.size%>); 

}); 

downvote.js.erb

$('.dislike').bind('ajax:success', function() { 

$('.up-<%= @debat.id %>').text(<%= @debat.get_upvotes.size%>); 
$('.down-<%= @debat.id %>').text(<%= @debat.get_downvotes.size%>); 

}); 

希望它有幫助!