我正在编写一个rails应用程序,就其本质而言,不需要用户注册,因此不能使用身份验证作为保护记录的常用方法.(我知道,我知道......)此处的用户信息仅限于电子邮件地址.所以我确实需要一种方法来使我的模型ID不可预测,以便其他ID不容易被猜到.(我知道我知道...)
我已经尝试使用像uuidtools这样的插件随机化id,因为创建了记录,如下所示:
require 'uuidtools'
class Post < ActiveRecord::Base
def before_create()
self.id = OpenSSL::Digest.SHA1.hexdigest(UUID.timestamp_create())
end
end
Run Code Online (Sandbox Code Playgroud)
......一开始看起来不错,但有趣的事情发生了.ActiveRecord有时会尝试在id中插入一个0值,我会收到错误,例如'找不到id = 0的帖子'等等...
我已经没想完了.有人可以帮忙吗?谢谢.
我用控制器中的'uuidtools'宝石这样:
def create
require 'uuidtools'
game = Game.new
game.permalink = Base64.encode64(UUIDTools::UUID.random_create)[0..8]
game.save
redirect_to :controller => 'home', :action => 'index'
end
Run Code Online (Sandbox Code Playgroud)
关于'uuidtools'的要求,我得到了这个错误:
no such file to load -- uuidtools
Run Code Online (Sandbox Code Playgroud)
(我将gem添加到我的gem文件中.)
我怎样才能解决这个问题?
谢谢,
俄德