[英]Rails Acts_as_votable Gem Like/Unlike Buttons with Ajax
[英]Second Like/Unlike button with acts_as_votable gem
我已经安装了acts_as_votable gem,并且试图实现两个按钮,一个按钮用于喜欢一个别针,一个按钮用于不喜欢一个针。 我已经设法创建了一个按钮来像别针,这是可行的。 但是,当我现在尝试添加第二个按钮时,我被卡住了。 任何帮助,不胜感激!
我的图钉索引视图
<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>
而我的Pins控制器:
def like
@pin.liked_by current_user
redirect_to :back
end
def unlike
@pin.unliked_by current_user
redirect_to :back
end
act_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>
对于unlike_pin_path
,请确保您的routes.rb
文件具有以下内容:
resources :pins do
member do
put "like", to: "pins#upvote"
put "unlike", to: "pins#downvote"
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.