Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails:用户向上投票的pin(存在?关系)_Ruby On Rails_Ruby_Vote_Vote Up Buttons - Fatal编程技术网

Ruby on rails rails:用户向上投票的pin(存在?关系)

Ruby 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

因此,我的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)
  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 %>