I have a helper method for ActiveAdmin that defines some actions that are the same across 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
On app boot, 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 do I define these methods across all the 'admin/*.rb' files? (This import functionality is the same across all models.)
I am using ruby 2.0 and rails 4.
EDIT:
When I define ImportHelper
in 'app/admin/import_helper.rb' like so:
# 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 'categories.rb' like so:
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 messy to me, as the Import file should not be in 'app/admin' and the require_relative
call should be unnecessary.
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
}
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.