Ruby on rails rails:用户向上投票的pin(存在?关系)
因此,我的rails应用程序中有一个非常简单的向上投票系统,允许用户向上投票pin: pins_controller.rbRuby on rails rails:用户向上投票的pin(存在?关系),ruby-on-rails,ruby,vote,vote-up-buttons,Ruby On Rails,Ruby,Vote,Vote Up Buttons,因此,我的rails应用程序中有一个非常简单的向上投票系统,允许用户向上投票pin: 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) el
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
在我看来,他们可以通过点击图标代表的按钮进行投票:
在app/views/pins/index.html.erb中
如果用户已经向上投票了PIN,我希望以不同的颜色呈现图标:
所以我想用exists
<%= 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 %>
但是is不工作,而是所有图标都显示为红色,有什么想法吗?问题是您正在检查表是否存在,请检查 您要做的是检查它是否为空
<%= 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 %>