ruby-on-rails – 用户无法使用Devise登录时重定向问题

在我的应用程序中,我使用Devise认证用户,我注意到,如果登录失败,您可以更改重定向到的页面.在维基上我找到了以下示例:
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文件夹(不知道放在哪里).这是我创建的以下类:

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状态应该完成):

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

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

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

找到此代码的维基是:How To: Redirect to a specific page when the user can not be authenticated

解决方法

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

编辑:要实现一个自动装载的lib,你可以将以下内容插入到你的application.rb中:

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

EDIT2:尝试重定向时忘记调用redirect_to.目前响​​应只返回root_url.尝试这个:

def respond
  if http_auth?
    http_auth
  else
    redirect_to root_url
  end
end

fyi:设计师们在重定向之前也执行此操作:

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...