Rails [已解决] Ruby on Rails Tutorial 6.3.2 章节密码和密码确认

mayday · 2013年03月16日 · 最后由 mayday 回复于 2013年03月16日 · 3395 次阅读

原书内容: 我们要把 password 和 password_confirmation 两列加入 User 模型,在记录存入数据库之前比较这两列的值是否一样。和之前见过的属性不一样, password 是虚拟的属性,只是临时存在于内存中,而不会存入数据库中。

问题: 所以说 User 里并没有 password 和 password_confirmation 这两列,那么 仅仅在 user.rb 中这样声明attr_accessible :email, :name, :password, :password_confirmation,就可以访问 password 这个属性? 例如书中测试文件中的:

before do 
    @user = User.new(name: "test", 
        email: "[email protected]",
        password:'foobar',
        password_confirmation:'foobar')
end

subject {@user}
it {should respond_to(:name)}
it {should respond_to(:password_digest)}
it {should respond_to(:password)}
it {should respond_to(:password_confirmation)}

我的测试是不通过的,error: ActiveRecord::UnknownAttributeError: unknown attribute: password

不知道大家的是什么情况?

牵涉章节,http://about.ac/rails-tutorial-2nd-cn/chapter6.html

很明显你漏了 has_secure_password

#1 楼 @AlphaLiu 有种默默的删帖的冲动~~~~ 谢了!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号