0

For example, I want to stop render layout for all actions. I can write in ApplicationController this code and its work:

layout :false

So, I want to create gem, which add this functionality. I try this code in lib of my gem:

module MyLayout
  class Engine < ::Rails::Engine
  end

  class ApplicationLayoutController < ApplicationController
    layout :false
  end
end

and this:

module MyLayout
  class Engine < ::Rails::Engine
  end

  class ApplicationController < ActionController::Base
    layout :false
  end
end

But it is not working. How can I do it?

user3599334
  • 87
  • 1
  • 7
  • 1
    Possible duplicate of [How can I extend ApplicationController in a gem?](http://stackoverflow.com/questions/11348332/how-can-i-extend-applicationcontroller-in-a-gem) – KARASZI István Nov 10 '15 at 13:20

1 Answers1

1

You are just defining your own ApplicationController class. It lives in your module, like so: MyLayout::ApplicationController. It doesn't affect the app that uses them gem by just existing.

If you want to provide the wanted functionality for users of your gem, you have a couple of options.

The "nicest" is probably to provide your own subclass of ActionController::Base and instruct your users to inherit from that:

module MyLayout
  class Engine < ::Rails::Engine
  end

  class ApplicationLayoutController < ApplicationController
    layout :false
  end
end

# When using your gem

class MyController < MyLayout::ApplicationLayoutController
  # stuff
end

Another way is to provide a module, which runs layout: false when included:

module MyLayout
  class Engine < ::Rails::Engine
  end

  module MyLayoutController
    def self.included(base)
      base.layout(:false)
    end
  end
end

# When using your gem

class MyController < ApplicationController
  include MyLayoutController
end

Another way, but probably not very advisable, is to monkey patch ActionController::Base.

Jesper
  • 4,535
  • 2
  • 22
  • 34