mahemoff
mahemoff

Reputation: 46509

ActionController Mixin

How could I create a mixin for action controller, which does something like:

layout Proc.new { |controller|
  if controller.request.xhr?
    'minimal'
  else
    'application'
  end
}

(I can't subclass ApplicationController, because I'm using a gem (Devise) which is tied to ActionController. A mixin seems more appropriate anyway.)

I've created a module called "XHRController" and used "ApplicationController::Base.include XHRController" in application.rb, but it errors on any use of "layout", "before_filter", etc. as being undefined.

Upvotes: 1

Views: 384

Answers (1)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

So, looks like you're looking to decide which layout to use. And you want to use 'minimal' if it's an AJAX request and otherwise use application. And you want the Devise views to also follow this same decision tree.

Seems like you could just have something like:

class ApplicationController < ActionController::Base

  layout :layout_decision_by_request_type

  def layout_decision_by_request_type
    if request.xhr?
      'minimal'
    else
      'application'
    end
  end

end

This page in the devise wiki also has two other options: https://github.com/plataformatec/devise/wiki/Custom-Layouts-for-Devise/22d024556aec73c8b65b630bd11a2e8ff7d17eaa

Upvotes: 1

Related Questions