Sinatra:使用会话进行数据库身份验证

时间:2011-07-27 19:59:18

标签: ruby sinatra authlogic

我正在编写一个小型的sinatra应用程序,我正在与Authlogic集成(https://github.com/ehsanul/Sinatra-Authlogic-Template之后)

除了我尝试登录时,一切正常。我收到以下错误:

NameError at /login
undefined local variable or method `active' for #<User:0x000001040208f0>

我包括authlogic gem而不是将其作为供应商包括在内。所以我的Sinatra应用程序与Github上的应用程序并不完全相同。

任何和所有的询问都将受到青睐!谢谢!

1 个答案:

答案 0 :(得分:1)

找到我的问题。

这是根据Github页面的模型:

class User < ActiveRecord::Base
  acts_as_authentic do |c|
    # Bcrypt is recommended
    #crypto_provider = Authlogic::CryptoProviders::BCrypt
    c.perishable_token_valid_for( 24*60*60 )
    c.validates_length_of_password_field_options =
     {:on => :update, :minimum => 6, :if => :has_no_credentials?}
    c.validates_length_of_password_confirmation_field_options =
     {:on => :update, :minimum => 6, :if => :has_no_credentials?}
  end

  def active?
    active
  end

  def has_no_credentials?
    crypted_password.blank? #&& self.openid_identifier.blank?
  end

  def send_activation_email
    Pony.mail(
      :to => self.email,
      :from => "no-reply@domain.tld",
      :subject => "Activate your account",
      :body =>  "You can activate your account at this link: " +
                "http://domain.tld/activate/#{self.perishable_token}"
    )
  end

  def send_password_reset_email
    Pony.mail(
      :to => self.email,
      :from => "no-reply@domain.tld",
      :subject => "Reset your password",
      :body => "We have recieved a request to reset your password. " +
               "If you did not send this request, then please ignore this email.\n\n" +
               "If you did send the request, you may reset your password using the following link: " +
                "http://domain.tld/reset-password/#{self.perishable_token}"
    )
  end
end

我删除了所有邮件方法,但我的脚本在active?方法失败了,因为它在users表中查找了一个活动列。由于我无法将此列附加到表中(由于与另一个系统的数据完整性),我只是告诉我的方法return true

我的User.rb

class UserSession < Authlogic::Session::Base
end

class User < ActiveRecord::Base
  acts_as_authentic do |c|

  end

  def active?
    return true
  end
end

希望这有助于某人!