2012-05-08 126 views
2

我正在學習Ruby on Rails。我正在創建一個用於存儲用戶信息的模型,並且在撥打rake db:seed時,我收到以下錯誤消息,想知道我缺少什麼?rake db:seed thorwing無法批量分配受保護的屬性

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 

回答

6

您必須添加password_confirmationattr_accessible

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

,它應該工作。

+0

@Gainster:確保使用正確的屬性名稱,謹防錯別字。 –

+0

我正在關注開始導軌3,它沒有製作字段attr_accessible https://gist.github.com/326271所以我不明白爲什麼我必須讓它們可訪問? – Gainster

+1

根據您使用的版本,Rails 3.2.3帶來了一項默認保護所有型號的更改。閱讀發行說明以獲取更多信息。 http://weblog.rubyonrails.org/2012/3/30/ann-rails-3-2-3-has-been-released/ – simonmorley

0

原木指出「不能大規模指派保護屬性:password_confrimation」 需要password_confrimation添加到用戶模型

attr_accessible:password_confrimation

相關問題