bga*_*oci 3 ruby authentication session blogs ruby-on-rails
我是新手,所以很容易.我创建了一个博客,能够通过使用像Facebook的"喜欢"这样的功能在帖子上"投票".我没有使用任何身份验证,但希望通过IP限制对特定帖子的投票.也就是说,一旦有人投票支持一个帖子,他们就不能再投票(除非他们当然重置了他们的路由器).
我觉得这应该是我通过修改投票或帖子模型而影响的东西,但我担心它与Sessions有关,但是......我还没有任何经验.
如果您需要我发布任何代码,请告诉我.这是投票控制者.
class VotesController < ApplicationController
def create
@post = Post.find(params[:post_id])
@vote = @post.votes.create!(params[:vote])
respond_to do |format|
format.html { redirect_to @post}
format.js
end
end
end
Run Code Online (Sandbox Code Playgroud)
有两种方法可以立即浮现在脑海中,可能还有其他方法.两者都需要在数据库中存储IP.
阻止使用唯一性验证创建投票.
class Vote < ActiveRecord::Base
validates_uniqueness_of :ip_address
...
end
Run Code Online (Sandbox Code Playgroud)阻止在控制器中创建投票
class VotesConroller < ApplicationController
...
def create
unless Vote.find_by_post_id_and_ip_address(params[:post_id],request.remote_ip)
posts.votes.create!(params[:vote].update({:ip_address => request.remote_ip}))
end
end
end
Run Code Online (Sandbox Code Playgroud)