ruby ActiveAdmin:在开发中,选择下拉菜单默认为当前值,但在生产中默认为空白

zqdjd7g9  于 2022-11-04  发布在  Ruby
关注(0)|答案(3)|浏览(115)

我有以下ActiveAdmin表单:

form do |f|
  f.inputs "Timesheet Details" do
    f.input :jobs_assigned_worker, :label => "Worker", as: :select, collection: Worker.all
    f.input :worked_time_hours,    :label => "Worked Time (Hours)"
    f.input :worked_time_mins,     :label => "Worked Time (Minutes)"
    f.input :driving_time_hours,   :label => "Driving Time (Hours)"
    f.input :driving_time_mins,    :label => "Driving Time (Minutes)"
    f.input :spent_dollars,        :label => "Extra Money Spent"
  end
  f.actions
end

当我在编辑视图中使用这个表单时,选择下拉菜单自动默认为当前值。但是在生产中,由于某种原因,下拉菜单默认为顶部的空白值(为什么会有空白值?)。

编辑

问题似乎是ActiveAdmin不理解关联,无法默认选择关联对象。我需要弄清楚如何为关联编写f.input。该表单用于时间表。一个时间表has_many JobsAssignedWorker,每个JobsAssignedWorker都有一个Worker。

ycl3bljg

ycl3bljg1#

如果要包括空白值:

f.input :jobs_assigned_worker,
  label: 'Worker',
  as: :select,
  collection: -> { Worker.pluck(:name) },
  include_blank: true

如果不想包含空白值:

f.input :jobs_assigned_worker,
  label: 'Worker',
  as: :select,
  collection: -> { Worker.pluck(:name) },
  include_blank: false

如果您希望有空白值,但不希望允许它作为选项:

f.input :jobs_assigned_worker,
  label: 'Worker',
  as: :select,
  collection: -> { Worker.pluck(:name) },
  include_blank: true,
  allow_blank: false
vkc1a9a2

vkc1a9a22#

请尝试设置“include_blank”选项。

form do |f|
    f.inputs "Timesheet Details" do
        f.input :jobs_assigned_worker, :label => "Worker", as: :select, collection: Worker.all, include_blank: false
        f.input :worked_time_hours,    :label => "Worked Time (Hours)"
        f.input :worked_time_mins,     :label => "Worked Time (Minutes)"
        f.input :driving_time_hours,   :label => "Driving Time (Hours)"
        f.input :driving_time_mins,    :label => "Driving Time (Minutes)"
        f.input :spent_dollars,        :label => "Extra Money Spent"
    end
    f.actions
end
tjjdgumg

tjjdgumg3#

要避免保留空白值,请选择此选项:

include_hidden: false

相关问题