layout false if request.xhr?

Posted by Justin Reagor Mon, 13 Jul 2009 14:30:00 GMT

Rather simple actually… since ActionController::Base.layout takes a proc, and calls it upon a request.

class ApplicationController < ActionController::Base
  def self.without_layout_on_xhr
    layout proc { |controller| controller.in_popup? ? nil : "application" }
  end

  def in_popup?
    request.xhr?
  end
end

class BusyWorkController < ApplicationController
  without_layout_on_xhr

  before_filter :login_required

  def new
    @work = Busy.new
  end
end

I’ve abstracted out the conditions for rendering without a template based on the controller instance method #in_popup?. Feel free to change it to whatever you want.