如何设置不要求用户登录的行为? [英] How do I set up acts as votable to not require user sign in?

查看:116
本文介绍了如何设置不要求用户登录的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试允许用户在无需登录/注册的情况下对线程进行投票,以提高用户参与度.我该怎么做呢?目前,我目前的流程是将投票与访问者的IP地址绑定在一起,以防止多次投票,尽管另一个问题是request.remote_ip不能为我获取正确的IP(我在学校网络中).有什么建议?非常感谢!

I'm trying to allow users to vote on threads without having to sign in/up in order to increase user engagement. How do I do this? At the moment my current though process is to tie votes with the visitor's IP address in order to prevent multiple votes, though another issue is that request.remote_ip is not getting me the correct IP (I'm on a school network). Any suggestions? Thanks very much!

在线程控制器中投票操作

Vote action in Threads Controller

def upvote
 @thread = Thread.find(params[:id])
 current_user.up_vote(@thread)
 flash[:message] = 'Thanks for voting!'
 respond_to do |format|
 format.html { redirect_to :back }
  format.js
  end

投票路线

resources :threads do
member do
  post :upvote
  post :unvote
end

推荐答案

我写的是votable.

I wrote acts as votable.

该宝石背后的全部想法是允许任何事物对任何事物进行投票(因此,对用户没有严格的依赖性,就像您所描述的情况一样).

The whole idea behind this gem was to allow anything to vote on anything (so there is not hard dependency on a user, like in the situation you are describing).

如果您不想使用IP地址存储选票,则可以使用与每个会话相关的唯一ID.这将对欺诈投票开放,但它将允许任何人无需账户就可以投票.

If you do not want to use IP addresses to store votes then maybe you can use a unique ID that you tie to each session. This would be open to vote fraud, but it would allow anyone to vote without having an account.

您可以做类似的事情

session[:voting_id] ||= create_unique_voting_id
voter = VotingSession.find_or_create_by_unique_voting_id(session[:voting_id])
voter.likes @thread

您需要设置一个VotingSession模型,该模型具有act_as_voter并维护唯一的投票ID,但这应该非常容易.

You would need to setup a VotingSession model that acts_as_voter and maintains a unique voting id, but that should be really easy w/ rails.

希望这会有所帮助

这篇关于如何设置不要求用户登录的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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