Ruby on rails 如何在Rails模型和表单中使用自定义函数?

Ruby on rails 如何在Rails模型和表单中使用自定义函数?,ruby-on-rails,activemodel,Ruby On Rails,Activemodel,我试图使用一个修改过的模型来支持bcrypt密码,就像这样 require 'bcrypt' class User < ActiveRecord::Base # users.password_hash in the database is a :string include BCrypt def password @password ||= Password.new(password_hash) end def password=(new_password)

我试图使用一个修改过的模型来支持bcrypt密码,就像这样

require 'bcrypt'
class User < ActiveRecord::Base
  # users.password_hash in the database is a :string
  include BCrypt

  def password
    @password ||= Password.new(password_hash)
  end

  def password=(new_password)
    @password = Password.create(new_password)
    self.password_hash = @password
  end
end
需要“bcrypt”
类用户
我在搭建脚手架之后添加了这个,希望password和password=足以让它继续运行,使用它,但是它不起作用,表单_for()等不起作用,我得到了错误:

ActionView::Template::Error (invalid hash):
2012-06-07T12:45:20+00:00 app[web.1]:     17:   </div>
2012-06-07T12:45:20+00:00 app[web.1]:     18:   <div class="field">
2012-06-07T12:45:20+00:00 app[web.1]:     19:     <%= f.label :password %><br />
2012-06-07T12:45:20+00:00 app[web.1]:     20:     <%= f.text_field :password %>
2012-06-07T12:45:20+00:00 app[web.1]:     21:   </div>
2012-06-07T12:45:20+00:00 app[web.1]:     22:   <div class="field">
2012-06-07T12:45:20+00:00 app[web.1]:     23:     <%= f.label :email %><br />
ActionView::Template::Error(无效哈希):
2012-06-07T12:45:20+00:00应用程序[网站1]:17:
2012-06-07T12:45:20+00:00应用程序[网站1]:18:
2012-06-07T12:45:20+00:00应用程序[网站1]:19:
2012-06-07T12:45:20+00:00应用程序[网站1]:20: 2012-06-07T12:45:20+00:00应用程序[网站1]:21: 2012-06-07T12:45:20+00:00应用程序[网站1]:22: 2012-06-07T12:45:20+00:00应用程序[网站1]:23:
有人知道我如何才能做到这一点吗?我是Rails新手

这是关于新功能的 按要求进行完全回溯:

2012-06-12T18:23:57+00:00 app[web.1]: ActionView::Template::Error (invalid hash):
2012-06-12T18:23:57+00:00 app[web.1]:     19:     <%= f.label :password %><br />
2012-06-12T18:23:57+00:00 app[web.1]:     18:   <div class="field">
2012-06-12T18:23:57+00:00 app[web.1]:     17:   </div>
2012-06-12T18:23:57+00:00 app[web.1]:     20:     <%= f.text_field :password %>
2012-06-12T18:23:57+00:00 app[web.1]:     22:   <div class="field">
2012-06-12T18:23:57+00:00 app[web.1]:     23:     <%= f.label :email %><br />
2012-06-12T18:23:57+00:00 app[web.1]:   app/models/user.rb:19:in `new'
2012-06-12T18:23:57+00:00 app[web.1]:     21:   </div>
2012-06-12T18:23:57+00:00 app[web.1]:   app/models/user.rb:19:in `password'
2012-06-12T18:23:57+00:00 app[web.1]:   app/views/users/_form.html.erb:20:in `block in _app_views_users__form_html_erb__2064609863987267967_31546180'
2012-06-12T18:23:57+00:00 app[web.1]:   app/views/users/_form.html.erb:1:in `_app_views_users__form_html_erb__2064609863987267967_31546180'
2012-06-12T18:23:57+00:00 app[web.1]:   app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___1991359801167056023_31763940'
2012-06-12T18:23:57+00:00 app[web.1]:   app/controllers/users_controller.rb:251:in `new'
2012-06-12T18:23:57+00:00应用程序[web.1]:操作视图::模板::错误(无效哈希):
2012-06-12T18:23:57+00:00应用程序[网站1]:19:
2012-06-12T18:23:57+00:00应用程序[网站1]:18: 2012-06-12T18:23:57+00:00应用程序[网站1]:17: 2012-06-12T18:23:57+00:00应用程序[网站1]:20: 2012-06-12T18:23:57+00:00应用程序[网站1]:22: 2012-06-12T18:23:57+00:00应用程序[网站1]:23:
2012-06-12T18:23:57+00:00应用程序[web.1]:app/models/user.rb:19:in'new' 2012-06-12T18:23:57+00:00应用程序[网站1]:21: 2012-06-12T18:23:57+00:00应用程序[网站1]:app/models/user.rb:19:在“密码”中 2012-06-12T18:23:57+00:00 app[web.1]:app/views/users/_form.html.erb:20:in'block in_app_view_users_form_html_erb2064609863987267967_31546180' 2012-06-12T18:23:57+00:00 app[web.1]:app/views/users/_-form.html.erb:1:在“app\u-views\u-users\u-form\u-html\u-erb\u 2064609863987267967\u 31546180”中 2012-06-12T18:23:57+00:00应用程序[web.1]:app/views/users/new.html.erb:3:在“应用程序视图用户新html”中 2012-06-12T18:23:57+00:00应用程序[web.1]:应用程序/控制器/用户/控制器。rb:251:在“新建”中
异常消息“invalid hash”表示您的视图工作正常,但是
User\35; password
由于任何原因失败。可能用户没有定义密码\u散列(即,它是nil,“,或其他一些无效值),导致
password.new
出错

查看您的回溯似乎强化了这一点:

2012-06-12T18:23:57+00:00 app[web.1]:   app/models/user.rb:19:in `new'
2012-06-12T18:23:57+00:00 app[web.1]:   app/models/user.rb:19:in `password'.
password
方法中调用
new
时,user.rb中出现错误。因此,我猜第19行是

因此它看起来像是
Password。出于某种原因,new
不喜欢
Password\u hash
的值。确切的原因,我只能猜测

  • 密码是什么
  • 密码散列的值是多少
异常消息“无效哈希”表示您的视图工作正常,但无论出于何种原因,
用户#密码
都会失败。可能用户没有定义密码\u散列(即,它是nil,“,或其他一些无效值),导致
password.new
出错

查看您的回溯似乎强化了这一点:

2012-06-12T18:23:57+00:00 app[web.1]:   app/models/user.rb:19:in `new'
2012-06-12T18:23:57+00:00 app[web.1]:   app/models/user.rb:19:in `password'.
password
方法中调用
new
时,user.rb中出现错误。因此,我猜第19行是

因此它看起来像是
Password。出于某种原因,new
不喜欢
Password\u hash
的值。确切的原因,我只能猜测

  • 密码是什么
  • 密码散列的值是多少

@JacobS添加了日志中的所有内容,根据您的日志更新了我的答案,并得出了更多的结论。好吧,密码是gem为bcrypt添加的,因为这是一个新字段,我猜它不像传递的是一个零/空白字段。Thankyou@JacobS添加了日志中的所有内容根据您的日志更新了我的答案,并给出了更多的结论。好吧,密码是gem为bcrypt添加的,因为这是一个新字段,我猜它不像传递的是一个nil/空白字段。谢谢你