英文:
How to automatically include a concern in all Active Admin Page controllers?
问题
I have the following concern:
./app/controllers/concerns/activity_helper.rb
module ActivityHelper
extend ActiveSupport::Concern
included do
before_action :my_method, except: %i[update destroy]
end
def my_method
...
end
end
Currently, I am including this concern in several Active Admin page controllers manually like so:
./app/admin/dashboard.rb
controller do
include ActivityHelper
end
I already tried the following but it does not work without giving any error:
- I created a new module in ./app/controllers/concerns/active_admin_controller_concern.rb:
module ActiveAdminControllerConcern
extend ActiveSupport::Concern
included do
controller do
include ActivityHelper
end
end
end
- I added the following method and config in ./config/initializers/active_admin.rb:
def add_active_admin_controller_concern
ActiveAdmin::ResourceDSL.send(:include, ActiveAdminControllerConcern)
end
ActiveAdmin.setup do |config|
config.before_action :add_active_admin_controller_concern
end
How to automatically include only the module ActivityHelper in all Active Admin page controllers?
英文:
I have the following concern:
./app/controllers/concerns/activity_helper.rb
module ActivityHelper
extend ActiveSupport::Concern
included do
before_action :my_method, except: %i[update destroy]
end
def my_method
...
end
end
Currently i am including this concern in several Active Admin page controllers manually like so:
./app/admin/dashboard.rb
controller do
include ActivityHelper
end
I already tried the following but it does not work without giving any error:
- I created a new module in ./app/controllers/concerns/active_admin_controller_concern.rb:
module ActiveAdminControllerConcern
extend ActiveSupport::Concern
included do
controller do
include ActivityHelper
end
end
end
- I added the following method and config in ./config/initializers/active_admin.rb:
def add_active_admin_controller_concern
ActiveAdmin::ResourceDSL.send(:include, ActiveAdminControllerConcern)
end
ActiveAdmin.setup do |config|
config.before_action :add_active_admin_controller_concern
end
How to automatically include only the module ActivityHelper in all Active Admin page controllers?
答案1
得分: 0
只需在文件./config/initializers/active_admin.rb
的末尾添加以下行:
ActiveSupport.on_load(:active_admin_controller) do
include ActivityHelper
end
英文:
You don't need the file ./app/controllers/concerns/active_admin_controller_concern.rb
Just add the following line at the end of the file ./config/initializers/active_admin.rb
ActiveSupport.on_load(:active_admin_controller) do
include ActivityHelper
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论