【问题标题】:Devise + Active Admin Redirect设计 + 主动管理员重定向
【发布时间】:2012-07-08 17:48:10
【问题描述】:

我在为我的应用程序设置重定向时遇到问题。用户应该转到他们的个人资料(用户/节目),管理员应该转到管理仪表板。我该如何设置?

目前出现以下错误:

 NameError in ActiveAdmin::Devise::SessionsController#create

    undefined local variable or method `admin' for #<ActiveAdmin::Devise::SessionsController:0x007febe12667e8>

应用控制器

def after_sign_in_path_for(resource_or_scope)
   if admin
   redirect_to admin_dashboard_path
  else
   @user
  end
 end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 devise activeadmin


    【解决方案1】:

    您没有要访问的admin 变量,您需要检查您获得的参数是什么。

    def after_sign_in_path_for(resource)
      stored_location_for(resource) ||
        if resource.is_a?(Admin)
          admin_dashboard_path
        else
          user_path(resource)
        end
    end
    

    你也应该在这个方法中重定向,它应该只返回一个设计可以使用的路径。

    【讨论】:

    • rails g active_admin:install 的默认设置创建了一个 AdminUser 类,所以我不得不将其修改为 if resource.is_a?(AdminUser)
    • 该死的,这可能只是为我节省了数小时的抓挠时间。非常感谢!
    • 我也是!感谢您花时间回答这个问题。 (我的也需要是 AdminUser)
    【解决方案2】:
    if resource.class == User
      root_path
    elsif resource.class == AdminUser
      admin_root_path
    else
    end
    

    【讨论】:

    • 虽然这段代码可能会回答这个问题,但最好在不介绍其他代码的情况下解释它是如何解决问题的,以及为什么要使用它。从长远来看,纯代码的答案没有用。另外,请确保在顶部格式化您的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多