简体   繁体   中英

ActiveAdmin :select drop-down defaults to current value in development but defaults to blank in production

I have the following ActiveAdmin form:

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

When I use this form in the edit view, the select drop-down automatically defaults to the present value. However in production the drop-down is for some reason defaulting to the blank value at the top (why is that blank value there anyway?).

EDIT

The problem seems to be that ActiveAdmin doesn't understand the association and is unable to select associated object by default. I need to figure out how to code the f.input for the association. The form is for a Timesheet. A Timesheet has_many JobsAssignedWorkers and each JobsAssignedWorker has a Worker.

If you want to include blank value:

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

If you don't want to include blank value:

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

If you want to have blank value, but don't want to allow it as an option:

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

Try to set 'include_blank' option.

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

to avoid persist the blank value just add in your select this option:

include_hidden: false

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM