2013-02-22 44 views
2

我一起this tutorial,並在一個點以下,它告訴我MassAssignmentSecurity錯誤而attr_accessible被稱爲

... add password and password_confirmation attributes to the User model [...] Unlike the other attributes we’ve seen so far, the password attributes will be virtual—they will only exist temporarily in memory, and will not be persisted to the database.

而且

As we’ll see in Section 6.3.4, these virtual attributes are implemented automatically by has_secure_password.

我的模型看起來是這樣的:

class User < ActiveRecord::Base 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    has_secure_password 
    attr_accessible :email, :is_admin, :name, :password, :password_confirmation 
    validates :name, presence: true, uniqueness: true 
    validates :password_digest, presence: true 
    validates :password, presence: true, length: { minimum: 6 } 
    validates :password_confirmation, presence: true 
    validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}, uniqueness: true 
end 

所以現在當我嘗試創建一個新的用戶;

User.create(name: "Foo Bar", email: "[email protected]", password: "foobar", password_confirmation: "foobar") 

我得到以下錯誤:

ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: password, password_confirmation 

爲什麼?

+3

我覺得這也應該工作。你做了平常的伎倆嗎?重新遷移數據庫並重新啓動rails服務器? – 2013-02-22 18:21:51

+0

啊,重新做了這個伎倆。謝謝! – Maarten 2013-02-22 18:47:36

+1

@garbagecollection您可以將您的評論推薦給答案,以便OP能夠接受它嗎? – alestanis 2013-02-27 11:07:17

回答

2

問題看起來好像密碼和password_confirmation列在數據庫中缺失。

根據評論,重新遷移並重新啓動服務器將解決此問題。

相關問題