Error adding ActiveAdmin Helper to instance_eval when turning on

I have a helper method for ActiveAdmin that defines some actions that are the same for all models.

In 'app / helpers / active_admin / import_helper.rb'

module ActiveAdmin module ImportHelper def self.included(base) base.instance_eval do action_item only: :index do link_to "Import", action: :import end collection_action :import do render "admin/import" end controller do def save_csvimport(item) # .. import stuff redirect_to action: :index end def permitted_params params.permit! end end end end end end 

In 'app / admin / categories.rb'

 ActiveAdmin.register Store::Category do include ImportHelper config.filters = false collection_action :importcsv, method: :post do save_csvimport "Category" end end 

When loading the application, I get the following error:

 app/helpers/active_admin/import_helper.rb:6:in `block in included': undefined method `action_item' for #<Module:0x007f93efabac40> (NoMethodError) 

How to define these methods in all "admin / *. Rb" files? (This import function is the same for all models.)

I am using ruby โ€‹โ€‹2.0 and rails 4.

EDIT:

When I define ImportHelper in 'app / admin / import_helper.rb' as follows:

 # Note no namespacing module ImportHelper def self.included(base) base.instance_eval do action_item only: :index do link_to "Import", action: :import end collection_action :import do render "admin/import" end controller do def save_csvimport(item) # .. Import stuff redirect_to action: :index end def permitted_params params.permit! end end end end end 

And the category.rb category looks like this:

 ActiveAdmin.register Store::Category do config.filters = false require_relative "./import_helper" include ImportHelper collection_action :importcsv, method: :post do save_csvimport "Category" end end 

Everything works. However, this seems to me useless, since the import file should not be in "app / admin", and the require_relative call is not required.

+4
source share
1 answer

Another approach:

 # This one needs to be loaded before every ActiveAdmin resource that is using it. I usually place it at: # app/admin/_defaults.rb module ActiveAdmin::Defaults def self.default_config(&block) proc{ breadcrumb do # ... end menu false before_filter :except => [:show] { # ... } controller do # ... end instance_exec(&block) if block_given? } end end # app/admin/users.rb ActiveAdmin.register User, &ActiveAdmin::Defaults.default_config{ index do # ... end show do # ... end } 
0
source

All Articles