Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 568
sixty4bit Avatar asked Aug 26 '14 00:08

sixty4bit


2 Answers

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
like image 185
Andrey Deineko Avatar answered Oct 30 '22 02:10

Andrey Deineko


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
like image 34
pragma Avatar answered Oct 30 '22 02:10

pragma