我想在登录页面上显示错误消息,例如“系统目前无法让您登录。请在登录过程中出现ldap错误时再试一次。”
目前,我使用了如上所述的custom_failure.rb方法 https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated 它能够捕获错误,如无效的用户名/密码,我已将其重定向到登录页面 根据需要显示错误消息没有问题。 但是这种方法无法捕捉到ldap错误。
这些ldap错误显示为非常原始的html页面,如下所示:
Errno :: Devise中的ETIMEDOUT :: SessionsController #create
连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应。 - 连接(2)
Rails.root:C:/extratxt/2.2.0/source/ruby/messaging 应用程序跟踪|框架跟踪|完整跟踪
请求
参数:
{"utf8"=>"✓",
"user"=>{"username"=>"administrator",
"password"=>"[FILTERED]",
"remember_me"=>"0"},
"commit"=>"Sign in"}
显示会话转储
显示env转储 响应
接头:
无
如何阻止此操作,而是显示我指定的页面(例如,我自己的错误消息的登录页面)? 任何的想法 ?感谢
答案 0 :(得分:2)
一种方法是覆盖创建会话操作,但需要几个步骤才能使其正常工作。首先,使用
复制设计视图(如果您还没有这样做)rails g devise:views
然后你需要创建一个扩展Devise :: SessionsController的sessions_controller.rb文件:
# /app/controllers/sessions_controller.rb
class SessionsController < Devise::SessionsController
# copied from [...]/gems/devise-1.4.5/app/controllers/sessions_controller.rb
# To find the path to your gem, use 'bundle show devise'
def create
begin
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => redirect_location(resource_name, resource)
rescue Net::LDAP::LdapError
# handle your error
respond_to do |format|
format.html { redirect_to new_user_session_path, :notice => $!.to_s }
format.json { render json: $!.to_s }
end
end
end
end
最后,在你的config / routes.rb中,你需要告诉devise使用你的自定义控制器:
devise_for :users, :controllers => { :sessions => :sessions }