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

标签 ruby-on-rails routes 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。

找到此代码的 wiki 位于:How To: Redirect to a specific page when the user can not be authenticated

最佳答案

试试把 custom_failure.rb走进你lib文件夹,因为它不是 helper 。然后确保文件已加载。您可能会尝试加载 lib 中的所有文件自动地。

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

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

编辑2 : 你忘了打电话redirect_to尝试重定向时。目前respond只返回 root_url .尝试这个:
def respond
  if http_auth?
    http_auth
  else
    redirect_to root_url
  end
end

仅供引用:设计人员在重定向之前也会执行此操作:
store_location!
flash[:alert] = i18n_message unless flash[:notice]

关于ruby-on-rails - 用户无法使用 Devise 登录时的重定向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4180386/

相关文章:

sql - 如何在单个查询中从 ActiveRecord 中的父项中选择唯一子项?

ruby-on-rails - 应该返回什么葡萄救援_从:all block?

ruby-on-rails - 设计路由: is there a way to remove a route from Rails. application.routes?

ruby-on-rails - 无法找到操作 'facebook' - devise/omniauth

ruby-on-rails-3 - 使用 Devise 创建新用户时跳过电子邮件确认

javascript - 编译后的文件没有获得指纹轨

ruby-on-rails - 谷歌管理目录插入用户

php - 带有问号的 Laravel 5.1 路由

Ember.js:一种实现真正全能路线的方法?

ruby-on-rails - Ruby on Rails : SSO implementation with LDAP using Devise gem