【问题标题】:rails: upvoted pin by user (exists? relation)rails:用户投票赞成的 pin(存在?关系)
【发布时间】:2014-10-28 21:50:19
【问题描述】:

所以我在我的 rails 应用中有一个非常简单的投票系统,它允许用户投票:

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

在我看来,他们可以通过单击按钮(由图标表示)来投票: 在 app/views/pins/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>

如果用户已经对图钉投了赞成票,我想用不同的颜色渲染图标: 所以我想到了使用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 %>

但是不起作用,而是所有图标都显示为红色,有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby vote vote-up-buttons


    【解决方案1】:

    问题是你正在检查表是否存在(检查exists? documents)。

    你要做的是检查它是否是empty?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 2018-07-08
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多