gpt4 book ai didi

ruby-on-rails - 调用 attr_accessible 时出现 MassAssignmentSecurity 错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:25 24 4
gpt4 key购买 nike

我正在关注 this tutorial有一次,它告诉我

... 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+\-.]+@[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: "foo@bar.net", password: "foobar", password_confirmation: "foobar")

我收到以下错误:

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

为什么?!

最佳答案

问题似乎是数据库中缺少 password 和 password_confirmation 列。

根据评论,重新迁移并重新启动服务器可以解决此问题。

关于ruby-on-rails - 调用 attr_accessible 时出现 MassAssignmentSecurity 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15030548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com