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?
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.