gpt4 book ai didi

ruby-on-rails - 无法批量分配 protected 属性 : confirmed_at

转载 作者:行者123 更新时间:2023-12-02 07:04:55 25 4
gpt4 key购买 nike

我正在使用 rails 中的设计身份验证创建一个演示应用程序

我遇到了这个错误

rake aborted!
Can't mass-assign protected attributes: confirmed_at

我的 User.rb 类是

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body

validates_presence_of :name
validates_uniqueness_of :name, :email, :case_sensitive => false
end

我的 db.seed.rb 文件代码是

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)

puts 'SETTING UP DEFAULT USER LOGIN'
user = User.create! :name => 'First User', :email => 'user@example.com', :password => 'please', :password_confirmation => 'please', :confirmed_at => DateTime.now
user2 = User.create! :name => 'Second User', :email => 'user2@example.com', :password => 'please', :password_confirmation => 'please', :confirmed_at => DateTime.now
puts 'New user created: ' << user.name

user.rb 是一个模型类。当我运行命令$ bundle exec rake db:seed

我遇到了这个错误 rake 中止! 无法批量分配 protected 属性:confirmed_at

最佳答案

您可以使用:

Model.create!(fieldsValues, :without_protection => true) 

在你的情况下:

User.create!({:name => 'First User', :email => 'user@example.com', :password => 'please', :password_confirmation => 'please', :confirmed_at => DateTime.now}, :without_protection => true)

without_protection 将允许您设置 protected 字段的值

关于ruby-on-rails - 无法批量分配 protected 属性 : confirmed_at,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14090365/

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