我如何为动作控制器创建一个mixin,它的功能类似于:
layout Proc.new { |controller|
if controller.request.xhr?
'minimal'
else
'application'
end
}(我不能子类化ApplicationController,因为我使用的是绑定到ActionController的gem (Devise)。mixin似乎无论如何都更合适。)
我已经创建了一个名为"XHRController“的模块,并在application.rb中使用了"ApplicationController::Base.include XHRController”,但它在使用"layout“、"before_filter”等时都会出错,因为它没有定义。
发布于 2011-12-19 10:51:07
所以,看起来您要决定使用哪种布局。如果是AJAX请求,你想使用'minimal‘,否则使用应用程序。并且您希望设计视图也遵循相同的决策树。
看起来你可以这样做:
class ApplicationController < ActionController::Base
layout :layout_decision_by_request_type
def layout_decision_by_request_type
if request.xhr?
'minimal'
else
'application'
end
end
enddevise wiki中的这个页面还有另外两个选项:https://github.com/plataformatec/devise/wiki/Custom-Layouts-for-Devise/22d024556aec73c8b65b630bd11a2e8ff7d17eaa
https://stackoverflow.com/questions/8554124
复制相似问题