3

私はレールでルビーを学んでいます。ユーザー情報を保存するためのモデルを作成していますが、 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
4

2 に答える 2

6

に追加する必要がありpassword_confirmationますattr_accessible

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

そしてそれは動作するはずです。

于 2012-05-08T06:28:56.517 に答える
0

ログに「保護された属性を一括割り当てできません:password_confrimation」と記載されているように、ユーザーモデルにpassword_confrimationを追加する必要があります

attr_accessible:password_confrimation

于 2014-02-24T09:06:19.077 に答える