【问题标题】:Authlogic: can't set password attribute from within classAuthlogic:无法从类中设置密码属性
【发布时间】:2010-02-14 20:40:23
【问题描述】:

我有一个用于AuthLogic's 密码管理的用户模型。 AuthLogic 在 db 支持的“crypted_pa​​ssword”属性之上添加了“password”和“password_confirmation”属性。这是非常标准的 AuthLogic 东西。

我想要一个同时设置密码和密码确认的方法(对于我不担心拼写错误的内部应用程序很有用)。为此,我在 User 中创建了一个新方法:

#user.rb
def password_and_confirm=(value)
  password = value
  password_confirmation = value
end

但是调用这个方法并没有 似乎真的设置了密码:

user = User.new
user.password = "test"
user.password               # => "test"
user.crypted_password       # => a big base64 string, as expected

user = User.new
user.password_and_confirm = "test"
user.password               # => nil
user.crypted_password       # => nil

我也尝试了不同的路线:

def internal_password(value)
  password = value
end

...遇到了同样的问题。

为什么我不能在 User 类的方法中设置密码属性?

【问题讨论】:

    标签: ruby-on-rails ruby passwords authlogic


    【解决方案1】:

    最好试试这个:

    #user.rb
    def password_and_confirm=(value)
      self.password = value
      self.password_confirmation = value
    end
    

    否则 ruby​​ 会尝试将方法(因为它是这样实现的)视为局部变量(这在赋值操作期间具有优先权)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-06
      • 2010-12-03
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      • 2017-07-17
      • 2013-07-26
      • 1970-01-01
      相关资源
      最近更新 更多