How to respond to HTML requests made via AJAX in Rails

后端 未结 4 381
一生所求
一生所求 2021-02-03 15:12

I\'m trying to write a Rails controller method that will respond to get requests made both \"normally\" (e.g. following a link) and via ajax.

Normal Case: The controller

4条回答
  •  迷失自我
    2021-02-03 16:00

    In your controller dynamically select whether to use a layout based on request.xhr?.

    For example:

    class SomeController < ApplicationController
      layout :get_layout
    
      protected
    
      def get_layout
        request.xhr? ? nil : 'normal_layout'
      end
    end
    

提交回复
热议问题