登录失败后设计重定向 [英] Devise redirect after login fail

查看:220
本文介绍了登录失败后设计重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现的所有问题都与帮助者成功登录有关
after_sign_in_path_for(资源)

All the questions I've found are related for a successful login with the helper after_sign_in_path_for(resource)

我在网站的索引中有一个登录表单,当登录失败时重定向到users / sign_in

I have a login form in the index of the site, and when the login fails it redirects to "users/sign_in"

但是如何重定向在登录失败时,到我的site#index?

But how can I redirect to my "site#index" when the login fails?

推荐答案


  1. 创建一个custom_failure .rb在您的lib目录中,其中:

  1. Create a custom_failure.rb in your lib directory, with:

class CustomFailure < Devise::FailureApp
  def redirect_url
    your_path
  end

  def respond
    if http_auth?
      http_auth
    else
      redirect
    end
  end
end


  • 在您的Devise初始化程序中,包括:

  • In you Devise initializer, include:

      config.warden do |manager|
        manager.failure_app = CustomFailure
      end
    


  • 确保Rails是加载你的lib文件,在你的application.rb:

  • Make sure Rails is loadin your lib files, in your application.rb :

    config.autoload_paths += %W(#{config.root}/lib)
    


  • 不要忘记重新启动服务器。

    Don't forget to restart your server.

    我不认为有更简单的方法来做到这一点。祝你好运。

    I don't think there's an easier way to do this. Good luck.

    这篇关于登录失败后设计重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆