10

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.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
sixty4bit
  • 7,422
  • 7
  • 33
  • 57
  • In development, what happens when there isn't a present value? Is it possible that in production you have a Worker with no label? – Mark Silverberg Aug 26 '14 at 01:35
  • Every Timesheet has to have a Worker associated with it – sixty4bit Aug 26 '14 at 01:56
  • 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 – sixty4bit Sep 08 '14 at 15:51

3 Answers3

23

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
GMchris
  • 5,439
  • 4
  • 22
  • 40
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
1

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
pragma
  • 1,290
  • 14
  • 16
0

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

include_hidden: false
Darlan Dieterich
  • 2,369
  • 1
  • 27
  • 37