我對rails有點新,我正在嘗試創建一個用戶登錄。我通過教程發現here。最後,我添加了「attr_accessible」進行批量分配。然而,當我這樣做,我得到了以下錯誤:未定義的方法attr_accessible
undefined method `attr_accessible' for #<Class:0x007ff70f276010>
我對這個post我neeed <的ActiveRecord :: Base的看到。但我確實包括這一點。這裏是我的用戶模型代碼:
class User < ActiveRecord::Base
attr_accessor :password
EMAIL_REGEX = /\A[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :password, :confirmation => true #password_confirmation attr
validates_length_of :password, :in => 6..20, :on => :create
before_save :encrypt_password
after_save :clear_password
attr_accessible :username, :email, :password, :password_confirmation
def encrypt_password
if password.present?
self.salt = BCrypt::Engine.generate_salt
self.encrypted_password= BCrypt::Engine.hash_secret(password, salt)
end
end
def clear_password
self.password = nil
end
end
任何其他想法是什麼可能會導致這個問題將非常感激,謝謝!
編輯:On Rails 4.1。看起來它不再適用。由於fotanus
[這裏閱讀](http://stackoverflow.com/questions/17371334/how-is-attr-accessible-used-in-rails-4)。用你正在使用的正確的rails版本修正你的問題,因爲這對於這個問題很重要。 – fotanus