简体   繁体   English

如何在Rails模型和form_for中使用自定义函数?

[英]How do you use a custom function in Rails Model and form_for?

I am trying to use a model altered to support bcrypt password, like so 我正在尝试使用更改为支持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

I added this in AFTER the scaffolding, hoping that the password and password= was enough to keep it going, using this, However it is not working, the form_for() etc wont work and I get the error: 我在脚手架之后添加了此内容,希望使用该密码和password =足以使其继续运行,但是却无法正常工作,form_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 />

Does anyone know how I can make this work, I am new to Rails 有谁知道我怎么做这项工作,我是Rails的新手

This is On the New Function Full backtrace as requested: 根据要求,这是在“新功能完全追溯”上:

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'

The exception message "invalid hash" indicates that your view is working just fine, but User#password fails for whatever reason. 异常消息“无效的哈希”表示您的视图运行良好,但是User#password出于任何原因均失败。 Perhaps user doesn't have a password_hash defined (ie, it is nil, "", or some other invalid value), causing Password.new to error out? 也许用户没有定义password_hash(即为nil,“”或其他无效值),从而导致Password.new出错了?

Looking at your backtrace seems to reinforce this: 查看您的回溯似乎可以加强这一点:

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'.

The error occurs in user.rb when calling new inside your password method. password方法内部调用new时,在user.rb中发生错误。 Thus I am guessing line #19 is 因此我猜第19行是

@password ||= Password.new(password_hash)

So it looks like Password.new does not like the value of password_hash for some reason. 因此,由于某种原因,看起来Password.new不喜欢password_hash的值。 Exactly why, I can only guess at. 确切为什么,我只能猜测。

  • What exactly is Password ? Password到底是什么?
  • What is the value of password_hash ? password_hash的值是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM