Never render a layout in response to xhrs

后端 未结 3 737
再見小時候
再見小時候 2020-12-06 17:44

Most of the time I don\'t want to render a layout when the request comes from AJAX. To this end I\'ve been writing render :layout => !request.xhr? frequently

3条回答
  •  广开言路
    2020-12-06 18:06

    A normal after_filter won't work because we want to modify rendering.

    How about hijacking render?

    class ApplicationController < ActionController::Base
    
      private
      def render(options = nil, extra_options = {}, &block) 
        options = {:layout => !request.xhr?}.merge(options) unless options.nil?
        super(options, extra_options)      
      end
    end
    

    Set the layout when calling render to override it. A bit ugly but should work.

提交回复
热议问题