2014-10-28 16 views
0

所以我在我的Rails應用程序一個非常簡單的給予好評的系統,它允許用戶給予好評引腳:導軌:upvoted引腳由用戶(存在關係?)

pins_controller.rb

def upvote 
    @pin = Pin.friendly.find(params[:id]) 

    if @pin.votes.create(user_id: current_user.id) 
    flash[:notice] = "Thanks for your recommendation." 
    redirect_to(pin_path) 
    else 
    redirect_to(pin_path) 
    end 
end 

而且我認爲它們可以通過點擊按鈕給予好評(由圖標表示):在應用程序/視圖/針/ index.html.erb

<%= link_to upvote_pin_path(pin), method: :post do %> 
    <span class="text-decoration: none;"><i class="fa fa-chevron-up"></i> 
<% end %> 
<%= pluralize(pin.votes.count, "") %></span> 

I W 烏爾德喜歡呈現不同的顏色的圖標,如果用戶已經upvoted引腳: 所以我想過使用存在?:

<%= link_to upvote_pin_path(pin), method: :post do %> 

     <% if pin.votes.where(user_id: current_user.id).exists? %> 
      <i class="fa fa-chevron-up" style="color:red"></i> 
     <% else %> 
      <i class="fa fa-chevron-up"></i> 
     <% end %> 
<% end %> 

但就是不工作,而不是所有的圖標顯示爲紅色,任何想法?

回答

0

問題是您正在檢查表是否存在(檢查exists? documents)。

你想要做的是檢查它是否是empty?

+0

謝謝你的男人:) – zacchj 2014-10-30 21:41:46