1
我試圖用改變來支持bcrypt密碼的模型,像這樣你如何在Rails模型和form_for中使用自定義函數?
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
我加入這個在腳手架後,希望將密碼和密碼=足以繼續下去,用這個,但是它不工作時,的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 />
有誰知道我怎樣才能使這項工作,我是新來的Rails
這是在新功能 完整的bac根據要求的ktrace:
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'
@JacobS添加了日誌中的所有內容 – nekosune
根據您的日誌更新了我的答案和更多結論。 –
密碼密碼是由寶石爲bcrypt添加的東西,因爲這是一個新的領域,我可以猜測它的dosnt像一個零/空白字段被傳遞。謝謝 – nekosune