上下文:为论坛创建tripcode实现(http://en.wikipedia.org/wiki/Tripcode)。基本上,无注册识别的弱哈希。
有一个模型,'发布'。帖子以父/子格式排列,新帖子创建父级,回复创建子级到父级。有一个表单,现在有一个字段发布到控制器/模型,包含内容和密码字段。
require 'bcrypt'
class Shout
include DataMapper::Resource
include BCrypt
property :id, Serial # unique key
property :content, Text
property :password_hash, String
property :trip, String # trip for display
belongs_to :forum
is :tree, :order => [:created_at]
attr_accessor :password
#before :save do
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
def trip
@trip = '!'<<self.password_hash.to_str[20..33]
self.trip = @trip
end
#end
end
DataMapper.finalize
基本流程就是这样 - 发布/回复,如果密码字段中有密码,则取出并运行bcrypt,将结果存储为password_hash供以后比较,创建tripcode进行显示。但是我遇到了错误,我一直在反对
我遇到的主要错误是
未定义的方法`原语?'为零:NilClass
似乎来自
lib / active_support / whiny_nil.rb:48:在`method_missing'
我不知道如何处理或解决这个问题。我没有做某事或用控制器检查一些东西,但还不知道是什么。我得到的另一个错误源于无效的bcrypt哈希,但不能立即复制它。
钩子方法就在bcrypt-ruby页面上。
创建BCryptHash字段有效(dm-types) - 但是在localhost服务器上将表单处理时间增加了10倍,并为每个帖子做了这样的事情,所以我需要一种方法来调整成本bcrypt哈希(例如1而不是默认值10)并且仅在存在密码时运行它,这就是我这样做的原因。
但是现在这种方法不起作用,我已经足够撞击它并转向其他问题并且如果我能得到一些输入那么回到它。我正在使用rails,所以我添加了该标签,尽管它主要不是rails问题。