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

查看:143
本文介绍了用户无法使用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

$ b以下这个例子我创建了自己的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.

编辑:实现一个自动加载的lib,您将插入以下内容到您的 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 。现在回复只返回 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:devise-guys还在重定向之前执行此操作:

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

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

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

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