加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:58:54 所属栏目:百科 来源:网络整理
导读:在我的应用程序中,我使用Devise认证用户,我注意到,如果登录失败,您可以更改重定向到的页面.在维基上我找到了以下示例: class CustomFailure Devise::FailureApp def redirect_url new_user_session_url(:subdomain = 'secure') end # You need to override
在我的应用程序中,我使用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]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读