【问题标题】:MassAssignmentSecurity error while attr_accessible has been called调用 attr_accessible 时出现 MassAssignmentSecurity 错误
【发布时间】:2013-02-08 10:43:23
【问题描述】:

我正在关注this tutorial,并且在某一时刻,它告诉我要这样做

... 将密码和密码确认属性添加到用户模型 [...] 与我们目前看到的其他属性不同,密码属性将是虚拟的——它们只会暂时存在于内存中,不会被持久化到数据库中。

正如我们将在第 6.3.4 节中看到的,这些虚拟属性由 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

为什么?!

【问题讨论】:

  • 我觉得这也应该有效。你做过惯常的把戏吗?重新迁移数据库并重新启动 rails 服务器?
  • 啊,重新迁移成功了。谢谢!
  • @garbagecollection 您能否将您的评论提升为答案,以便 OP 接受它?

标签: ruby-on-rails ruby passwords


【解决方案1】:

问题似乎数据库中缺少密码和密码确认列。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2017-03-06
    • 1970-01-01
    • 2012-03-23
    相关资源
    最近更新 更多