1

上下文:为论坛创建一个tripcode 实现(http://en.wikipedia.org/wiki/Tripcode)。本质上,用于无注册识别的弱散列。

有一个模型,“Post”。帖子以父/子格式排列,新帖子创建父级,回复创建子级到父级。有一个表单,现在有一个发布到控制器/模型的字段,包含一个内容和密码字段。

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:in `method_missing'

我不知道如何处理或解决这个问题。我没有用控制器做某事或检查某事,但还不知道是什么。我得到的另一个错误源于无效的 bcrypt 哈希,但无法立即复制。

钩子方法就在 bcrypt-ruby 页面上。

创建 BCryptHash 字段有效(dm-types)——但在 localhost 服务器上将处理表单的时间增加了 10 倍,并且对每个帖子都执行此操作,因此我需要一种方法来调整 bcrypt 哈希的成本(例如 1 而不是默认的 10)并且仅在存在密码时运行它,这就是我这样做的原因。

但这现在行不通,我已经把头撞得够多了,然后继续解决其他问题,如果我能得到一些意见,就会回到它。我正在使用 rails,所以我添加了该标签,尽管它主要不是 rails 问题。

4

1 回答 1

0

随时在此处查看或贡献或使用错误。

https://github.com/blueblank/Shout/tree/oneshout

于 2010-12-03T18:11:41.643 回答