我对rails有些陌生,我正在try 创建一个用户登录.我浏览了教程,找到了here个.最后,它让我为大规模作业(job)添加了"attr_accessible".然而,当我这样做时,我得到了以下错误:
undefined method `attr_accessible' for #<Class:0x007ff70f276010>
我在这post上看到了我需要的<;ActiveRecord::Base.但我也包括在内.以下是我的用户模型的代码:
class User < ActiveRecord::Base
attr_accessor :password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[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
如果您有任何关于导致此问题的其他 idea ,我们将不胜感激,谢谢!
编辑:在Rails 4.1上.看起来已经不适用了.谢谢福塔努斯