10

我有以下 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_manyJobsAssignedWorkers 并且每个 JobsAssignedWorker 都有一个 Worker。

4

2 回答 2

24

如果要包含空白值:

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
于 2014-08-26T11:48:40.590 回答
1

尝试设置“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
于 2014-08-26T06:15:32.910 回答