Search code examples
ruby-on-railsrubylayoutruby-on-rails-3responders

Set layout from responder?


I am trying to figure out how to set the layout from a custom made responder. I want to use the request.xhr? to set the layout for render to 'ajax'. Does anybody know how to do that? I am using Rails 3 and I have a responder like this:

module AjaxLayoutResponder
  def to_html
    if request.xhr?
      # do something here to change layout...
    end
    super
  end
end

It seem to me like a responder is the best way to accomplish this 'ajax' layout switching.


Solution

  • I disagree that a responder is the way to go. Here's an easy solution that I use in most of my projects (however I just set the ajax layout to nil):

    In application_controller.rb

    layout :set_layout
    
    def set_layout
      request.xhr? 'ajax' : 'application'
    end