Formtastic/ActiveAdmin set a default value for a select input

Morad Edwar picture Morad Edwar · Jul 9, 2017 · Viewed 7.9k times · Source

I am working on a custom form in ActiveAdmin which I decided to use it for adding/editing to follow the DRY principle so I need to populate it if the user uses it for editing a record ( which isn't a DB record ).

So the problem is I have those inputs :

f.input :model_id, as: :select, collection: Model.all.map { |m| [m.id.to_s + ' - ' + m.name, m.id] }, input_html: { required: true }
f.input :enabled, as: :select, collection: {'Yes': true, 'No': false}, input_html: { required: true }

And I'd like to set a default value for them if I'm using the form for editing but I failed to know how because everybody is talking about using belongs_to or a DB relation and ActiveAdmin will take care of the default value for you which isn't applicable for my case because it's not a DB record and I don't have an ActiveRecord Model for it.

Even the official docs of Formtastic didn't help.

Answer

Morad Edwar picture Morad Edwar · Jul 9, 2017

I figured out how to do it and here is the answer for anyone facing the same problem :

f.input :model_id, collection: Model.all.map { |m| [m.id.to_s + ' - ' + m.name, m.id] }, selected: object.model_id
f.input :enabled, collection: { 'Yes': true, 'No': false }, selected: object.enabled