【问题标题】:unique after_sign_out paths with multiple models with Devise and rails具有 Devise 和 rails 的多个模型的唯一 after_sign_out 路径
【发布时间】:2012-12-13 08:40:28
【问题描述】:

我有一个带有 Devise 2.1 的 rails 3.2 应用程序

我有 2 个使用设计的模型(AdminUser 和 User)

型号:

class AdminUser < ActiveRecord::Base
    devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable
end

class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
end

我已经通过设计生成器为两个模型生成了单独的视图。 AdminUser 的views/devise 文件夹(在新要求之前几个月实施) 用户模型的views/users文件夹

退出后,我想重定向到与设计模型匹配的特定操作。下面的代码在 application_controller.rb 中工作,但它适用于我不想做的两个模型:

def after_sign_out_path_for(user)
  user_landing_path
end

退出任何一个模型都会重定向到同一个登录页面,但我希望两个设计模型都有一个唯一的目的地。

我怎样才能做到这一点?

【问题讨论】:

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


    【解决方案1】:

    在查看了此处的一些示例后,我发现了似乎是一种解决方案 http://eureka.ykyuen.info/2011/03/10/rails-redirect-previous-page-after-devise-sign-in/

    def after_sign_out_path_for(resource_or_scope)
      case resource_or_scope
        when :user, User
          user_landing_path
        when :admin_user, AdminUser
          admin_user_landing_path
        else
          super
      end
    end
    

    【讨论】:

      【解决方案2】:

      你可以做的一些事情:

      case user.class
      when AdminUser.class
        do_admin_sign_out()
      when User.class
        do_user_sign_out()
      else
        no_idea_who_you_are()
      end
      

      if user.kind_of? AdminUser
        do_admin_thing()
      else
        do_user_thing()
      end
      

      或者,您可以为两个模型添加admin? 检查,并检查,即:

      if user.admin?
        do_admin_thing()
      ...
      

      我可能会选择后者,因为这可能会出现在其他地方,但这些都是您的选择。

      【讨论】:

      • 这段代码在设计退出序列的上下文中会去哪里?我可以看到检查是在临时基础上进行的,但不确定它是否适合我在设计引擎中使用控制器的这种情况
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 2015-01-07
      相关资源
      最近更新 更多