错误信息
Started POST "/admins/sign_in" for 127.0.0.1 at 2012-02-08 10:56:11 +0800
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"2EGtbBAvYheW53Bxx6VEK3vqyE6FjlF7obbhKGhLNJE=", "login"=>"[email protected]", "admin"=>{"password"=>"[FILTERED]"}}
Completed 401 Unauthorized in 6ms
admin.rb
attr_accessible :email, :username, :password, :password_confirmation, :remember_me
attr_accessor :login
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", {:value => login.strip.downcase}]).first
end
protected
# Attempt to find a user by it's email. If a record is found, send new
# password instructions to it. If not user is found, returns a new user
# with an email not found error.
def self.send_reset_password_instructions(attributes={})
recoverable = find_recoverable_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
recoverable.send_reset_password_instructions if recoverable.persisted?
recoverable
end
def self.find_recoverable_or_initialize_with_errors(required_attributes, attributes, error=:invalid)
(case_insensitive_keys || []).each { |k| attributes[k].try(:downcase!) }
attributes = attributes.slice(*required_attributes)
attributes.delete_if { |key, value| value.blank? }
if attributes.size == required_attributes.size
if attributes.has_key?(:login)
login = attributes.delete(:login)
record = find_record(login)
else
record = where(attributes).first
end
end
unless record
record = new
required_attributes.each do |key|
value = attributes[key]
record.send("#{key}=", value)
record.errors.add(key, value.present? ? error : :blank)
end
end
record
end
def self.find_record(login)
where(["username = :value OR email = :value", {:value => login}]).first
end
end
View
<input id="admin_login" name="login" size="30" type="text" class="validate[required]"/>
<input id="admin_password" name="admin[password]" size="30" type="password" class="validate[required]"/>
devise.rb
config.authentication_keys = [ :login ]