我已经安装了acts_as_votable宝石,我正在尝试实现两个按钮,一个用于喜欢一个引脚,另一个用于取消引脚。我已经设法创建一个像针一样的按钮,这是有效的。但是,当我现在尝试添加第二个按钮时,我会卡住。任何帮助非常感谢!!
我的图钉索引视图
<div class="actions">
<% if user.likes? @pin%>
<%= link_to unlike_pin_path(pin), method: :put, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
<% end %>
<% else %>
<%= link_to like_pin_path(pin), method: :put, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart-empty"></span>I like
<% end %>
<% end %>
</div>
我的引脚控制器:
def like
@pin.liked_by current_user
redirect_to :back
end
def unlike
@pin.unliked_by current_user
redirect_to :back
end
答案 0 :(得分:1)
acts_as_votable gem不提供likes?
方法。它确实提供了liked?
方法。没有经过测试我想你想使用以下代码:
<div class="actions">
<% if user.liked? @pin %>
<%= link_to unlike_pin_path(pin), method: :put, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
<% end %>
<% else %>
<%= link_to like_pin_path(pin), method: :put, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart-empty"></span>I like
<% end %>
<% end %>
</div>
答案 1 :(得分:1)
对于unlike_pin_path
,请确保您的routes.rb
文件包含以下内容:
resources :pins do
member do
put "like", to: "pins#upvote"
put "unlike", to: "pins#downvote"
end
end