【问题标题】:How to define custom Failure for devise in case of two different models User and active admin?如果有两个不同的模型用户和活动管理员,如何为设计定义自定义失败?
【发布时间】:2013-02-12 09:49:48
【问题描述】:

我有两个模型 User 和 ActiveAdmin 我想在其上应用我的 devise 集成。

我的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 中定义:

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 login 或admin login?根据当前情况,它总是重定向到用户登录页面??

【问题讨论】:

    标签: ruby-on-rails devise customization activeadmin


    【解决方案1】:

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

    然后Redirect to a specific page。

    更新:

    你必须使用 scope 来解决这个问题:-

    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 
    

    【讨论】:

    • 我已经这样做了。我担心的是如何识别此自定义故障是针对 user 或 ActiveAdmin
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 2012-03-23
    • 1970-01-01
    • 2013-06-01
    • 2012-08-24
    相关资源
    最近更新 更多