gpt4 book ai didi

ruby-on-rails - 包含时,ActiveAdmin Helper 在 instance_eval 上抛出错误

转载 作者:数据小太阳 更新时间:2023-10-29 07:39:36 26 4
gpt4 key购买 nike

我有一个用于 ActiveAdmin 的辅助方法,它定义了一些在所有模型中都相同的操作。

在“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

在“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

在应用程序启动时,我收到以下错误:

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

如何在所有“admin/*.rb”文件中定义这些方法?(此导入功能在所有模型中都是相同的。)

我正在使用 ruby​​ 2.0 和 rails 4。

编辑:

当我像这样在“app/admin/import_helper.rb”中定义 ImportHelper 时:

# 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

'categories.rb' 是这样的:

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

一切正常。然而,这对我来说似乎很困惑,因为导入文件不应该在“app/admin”中,并且 require_relative 调用应该是不必要的。

最佳答案

另一种方法:

# 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
}

关于ruby-on-rails - 包含时,ActiveAdmin Helper 在 instance_eval 上抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19014597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com