actions_as_votable大拇指向上/向下按钮 [英] acts_as_votable thumbs up/down buttons

查看:105
本文介绍了actions_as_votable大拇指向上/向下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了acts_as_votable gem,它在控制台中可以正常工作(如文档中所述). 所以我的问题是如何为upvote和downvote按钮设置表单?还是仅仅是链接?

I installed the acts_as_votable gem, it works in the console like it should (like it says in the documentation). So my question is how to set up a form for upvote and downvote buttons? or can they simply be links?

这是文档:github.com/ryanto/acts_as_votable/blob/master/README.markdown

here is the documentation: github.com/ryanto/acts_as_votable/blob/master/README.markdown

我有一个用户和一个图片模型;用户应该能够喜欢该图片. 图片视图中的代码,其中的按钮应为:

I have a user and a picture model; the user is supposed to be able to like the picture. code from the picture view, where the buttons should be:

<% for picture in @pictures %> 
<p> 
<%= image_tag picture.image_url(:thumb).to_s %> 
</p> 
<%= picture.created_at.strftime("%a, %d %b. %Y") %>, by 
<%= link_to picture.user.name, picture.user %> 
<h2> <%= link_to picture.name, picture %></h2> 

[buttons here] 

<%= picture.votes.size %> <% end %>

推荐答案

做到这一点的一种方法是添加自己的上下表决控制器动作. 我假设您的控制器中有一个current_user方法.

One way to do this is to add your own controller actions for up- and downvotes. I'm assuming you have a current_user method available in your controller.

# pictures_controller.rb
def upvote
  @picture = Picture.find(params[:id])
  @picture.liked_by current_user
  redirect_to @picture
end

def downvote
  @picture = Picture.find(params[:id])
  @picture.downvote_from current_user
  redirect_to @picture
end

# config/routes.rb

resources :pictures do
  member do
    put "like", to: "pictures#upvote"
    put "dislike", to: "pictures#downvote"
  end
end

# some view

<%= link_to "Upvote", like_picture_path(@picture), method: :put %>
<%= link_to "Downvote", dislike_picture_path(@picture), method: :put %>

这篇关于actions_as_votable大拇指向上/向下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆