本文介绍了在两个不同的模型用户和活动管理员的情况下,如何为设计定义自定义失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型User和ActiveAdmin,我想在这些模型上应用我的devise集成.

I have two models User and ActiveAdmin on which I want to apply my devise integrations.

我的custom_failure.rb如下

class CustomFailure < Devise::FailureApp

  def redirect_url
    login_path
  end

  # def redirect_url
  #   root_path
  # end

  def respond
    if http_auth?
      http_auth
    else
      redirect
    end
  end
end

似乎效果很好.

此外,可以在我的application controller中进行定义,例如:

Also, can define in my application controller like :

def after_sign_in_path_for(resource)
  # case resource
    if resource.is_a?(Admin)
      admin_dashboard_path
    else
      root_path
    end
end

def after_sign_out_path_for(resource_or_scope)
  login_path
end

但是问题是如何在custom_failure.rb中使用此resource以便我可以相应地重定向到user loginadmin login的登录名?根据当前情况,它始终会重定向到用户登录页面??

But the issue is how to use this resource in custom_failure.rb so that I can redirect accordingly to login for user login or for the admin login ?? Acc to current scenario it always redirects to the user login page ??

推荐答案

尝试将custom_failure.rb放入您的lib文件夹.然后确保文件已加载.可能您会尝试自动加载lib中的所有文件.

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

然后重定向到特定页面.

更新:

您必须使用 scope 来解决此问题:-

You have to use scope to solve this :-

class CustomFailure < Devise::FailureApp 
  def redirect_url 
    if warden_options[:scope] == :user 
      signin_path 
    else 
      new_admin_user_session_path 
    end 
  end 
  def respond 
    if http_auth? 
      http_auth 
    else 
      redirect 
    end 
  end 
end 

这篇关于在两个不同的模型用户和活动管理员的情况下,如何为设计定义自定义失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 17:25