Never render a layout in response to xhrs

后端 未结 3 730
再見小時候
再見小時候 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 17:48

    In order to make it the default to never render a layout for any XHR request, you can do this:

    class ApplicationController < ActionController::Base
      layout proc { false if request.xhr? }
    end
    

    When the request is an XHR request, it renders the requested view without a layout. Otherwise, it uses the default layout behaviour, which looks up the layout by inheritance.

    This is different than saying controller.request.xhr? ? false : 'application', since that will always render the application layout for a non-XHR request, which effectively disables the lookup by inheritance.

    Also see the ActionView documentation for the nil argument and layout inheritance.

    0 讨论(0)
  • 2020-12-06 17:52

    How about this?

    class UsersController < ApplicationController
      layout proc {|controller| controller.request.xhr? ? false : "application" }
    end
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题