gpt4 book ai didi

ruby-on-rails - rake 数据库 :seed thorwing Can't mass-assign protected attributes

转载 作者:数据小太阳 更新时间:2023-10-29 07:50:41 25 4
gpt4 key购买 nike

我正在学习 ruby​​ on rails。我正在创建一个用于存储用户信息的模型,当调用 rake db:seed 时出现以下错误,知道我遗漏了什么吗?

rake db:seed

require 'digest'
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :email, :password
validates :email, :uniqueness => true,
:length => {:within => 5..50},
:presence => true

validates :password, :confirmation => true, :length => { :within => 4..20 }, :presence => true, :if => :password_required?

has_one :profile

has_many :articles, :order => 'published_at DESC, title ASC',
:dependent => :nullify
has_many :replies, :through => :articles, :source => :comments

before_save :encrypt_new_password

def self.authenticate(email, password)
user = find_by_email(email)
return user if user && user.authenticated?(password)
end

def authenticated?(password)
self.hashed_password == encrypt(password)
end


def encrypt_new_password
return if password.blank?
self.hashed_password = encrypt(password)
end

def password_required?
hashed_password.blank? || password.present?
end

def encrypt(string)
Digest::SHA2.hexdigest(string)
end

end




Can't mass-assign protected attributes: password_confrimation
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/base.rb:498:in `initialize'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/persistence.rb:44:in `new'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/persistence.rb:44:in `create'
C:/Users/huzaifa.gain/My Documents/Aptana Studio 3 Workspace/blog/db/seeds.rb:6:in `<top (required)>'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:in `load'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:245:in `block in load'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:23

最佳答案

您必须将 password_confirmation 添加到 attr_accessible

class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :email, :password, :password_confirmation

它应该可以工作。

关于ruby-on-rails - rake 数据库 :seed thorwing Can't mass-assign protected attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10493640/

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