【问题标题】:ActionController MixinActionController Mixin
【发布时间】:2012-01-23 03:47:20
【问题描述】:

我如何为动作控制器创建一个 mixin,它的作用类似于:

layout Proc.new { |controller|
  if controller.request.xhr?
    'minimal'
  else
    'application'
  end
}

(我不能继承 ApplicationController,因为我使用的是与 ActionController 绑定的 gem(设计)。无论如何,mixin 似乎更合适。)

我创建了一个名为“XHRController”的模块并在 application.rb 中使用了“ApplicationController::Base.include XHRController”,但在任何使用“layout”、“before_filter”等时都会出错,因为未定义。

【问题讨论】:

    标签: ruby-on-rails mixins actioncontroller


    【解决方案1】:

    因此,您似乎正在决定使用哪种布局。如果它是 AJAX 请求,您想使用“最小”,否则使用应用程序。并且您希望设计视图也遵循相同的决策树。

    好像你可以有类似的东西:

    class ApplicationController < ActionController::Base
    
      layout :layout_decision_by_request_type
    
      def layout_decision_by_request_type
        if request.xhr?
          'minimal'
        else
          'application'
        end
      end
    
    end
    

    设计 wiki 中的此页面还有两个其他选项:https://github.com/plataformatec/devise/wiki/Custom-Layouts-for-Devise/22d024556aec73c8b65b630bd11a2e8ff7d17eaa

    【讨论】:

    • 谢谢杰西。这是其中的一部分,我还使用 before_filter 插入自定义标题。
    猜你喜欢
    • 1970-01-01
    • 2018-11-02
    • 2022-01-14
    • 1970-01-01
    • 2011-03-22
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多