Rails ActiveAdmin batch_action 的表单导致资产:预编译错误

问题描述 投票:0回答:1

我们使用Rails ActiveAdmin,并且遇到奇怪的资产编译错误。该错误导致我们的 rspec CI 作业在 github 中失败。它因数据库连接错误而失败,如下所示:

Run bundle exec rake assets:precompile rake aborted!
ActiveRecord::DatabaseConnectionError: There is an issue connecting to your database with your username/password, username: root.
Please check your database configuration to ensure the username/password are valid.

所以看起来我们在 ci 数据库配置中做了一些愚蠢的事情,但是......奇怪的是,只有当我们尝试在 ActiveAdmin 代码中执行非常具体的操作时,才会发生此错误: 我们正在尝试添加一个带有“form”参数的新“batch_action”。

batch_action :migrate_employees, confirm: 'Select target Organisation:', form: { 'Select Organisation': Organisation.enabled.not_for_demo.pluck(:name, :id), 'Remove Validation Number': :checkbox } do |ids, inputs| notice = "TEST#{ids},#{inputs}" redirect_to admin_users_path, notice: notice end

上述内容在本地开发中效果很好。如果我们取出“form”参数,那么它也会按预期工作(github 构建资产并在我们的整个大代码库上运行 rspec 测试),但有了这一点,它奇怪地决定它不再知道如何连接到数据库在资产期间:预编译。

有谁能告诉我们我们需要在哪里解决这个问题吗?

ruby-on-rails activeadmin
1个回答
0
投票
'Select Organisation': Organisation.enabled.not_for_demo.pluck(:name, :id),

。它在开发中可以工作,因为应用程序已加载并可以访问您的配置开发数据库,但您应该将其视为“偶然工作”。预编译正在尝试构建表单,并且它按照代码中的指令立即访问数据库并使用该结果来定义静态选择列表。您需要将查询封装在过程中,以防止它在您不需要该数据时触发。

ActiveAdmin 的批量操作表单文档中也提到了这一点:

当您有动态表单输入时,您可以传递一个过程:

batch_action :doit, form: -> { {user: User.pluck(:name, :id)} } do |ids, inputs|


© www.soinside.com 2019 - 2024. All rights reserved.