用户无法使用 Devise 登录时的重定向问题 [英] Redirecting issues when user cannot sign in using Devise

查看:15
本文介绍了用户无法使用 Devise 登录时的重定向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用 Devise 对用户进行身份验证,我注意到如果登录失败,您可以更改重定向到的页面.在 wiki 上,我找到了以下示例:

In my application I authenticate users using Devise and I noticed that you can change the page that one is redirected to if the sign in fails. On the wiki I found the following example:

class CustomFailure < Devise::FailureApp
  def redirect_url
    new_user_session_url(:subdomain => 'secure')
  end

  # You need to override respond to eliminate recall
  def respond
    if http_auth?
      http_auth
    else
      redirect
    end
  end
end

按照这个例子,我创建了自己的 CustomFailure 类 (custom_failure.rb) 并放置在 helper 文件夹中(不知道放在哪里).这是我创建的以下类:

Following this example I created my own CustomFailure class(custom_failure.rb) and placed in the helper folder (not sure where to put it). This is the following class I created:

class CustomFailure < Devise::FailureApp
  def redirect_url
    new_user_session_url(:subdomain => 'secure')
  end

  # Redirect to root_url
  def respond
    if http_auth?
      http_auth
    else
      root_url
    end
  end
end

我还在 config/initializers/devise.rb 文件中添加了以下内容(因为 wiki 声明应该完成):

I also added the following in the config/initializers/devise.rb file (as the wiki states should be done):

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

虽然我没有收到任何错误,但当我错误登录时,它仍然重定向到/users/sign_in 页面(不是根页面)并且没有加载任何内容(即使源不为空,页面也完全是白色的).我的 CustomFailure 类是否有问题,或者它位于错误的文件夹中?

Although I get no errors, when I incorrectly sign in it still redirects to the /users/sign_in page (not the root page) and nothing is loaded (the page is completely white even though the source is not empty). Is there comthing wrong with my CustomFailure class or maybe it is in the wrong folder?

我使用的是 Rails 3.0.1 和 Devise 1.1.rc0.

I am using Rails 3.0.1 and Devise 1.1.rc0.

此代码所在的 wiki 位于:如何:当用户无法通过身份验证时重定向到特定页面

The wiki where this code is found is at: How To: Redirect to a specific page when the user can not be authenticated

推荐答案

尝试将 custom_failure.rb 放入您的 lib 文件夹中,因为它不是一个帮助程序.然后确保文件已加载.可能您会尝试自动加载 lib 中的所有文件.

Try putting custom_failure.rb into you lib folder as it is not a helper. Then make sure the file is loaded. Probably you would attempt to load all files in lib automatically.

编辑:要实现自动加载的库,您需要将以下内容插入到您的 application.rb 中:

EDIT: To achieve an auto loaded lib you would insert the following to your application.rb:

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

EDIT2:您在尝试重定向时忘记调用 redirect_to.目前respond 只返回root_url.试试这个:

EDIT2: you forgot to call redirect_to when trying to redirect. At the moment respond only returns root_url. Try this:

def respond
  if http_auth?
    http_auth
  else
    redirect_to root_url
  end
end

仅供参考:设计人员也在重定向之前执行此操作:

fyi: the devise-guys also perform this before redirecting:

store_location!
flash[:alert] = i18n_message unless flash[:notice]

这篇关于用户无法使用 Devise 登录时的重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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