Formtastic / ActiveAdmin sets default value for selection input
I am working on a custom form in ActiveAdmin that I decided to use for add / edit to follow the principle DRY
, so I need to fill it in if the user is using it to edit a record (which is not a DB).
So, the problem is I have these 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 would like to set a default for them if I use an edit form, but I didn't know how, because everyone is talking about belongs_to
DB usage or relationship, and ActiveAdmin
would care about default values ββfor you, which is not applicable for my case because it is not a database entry and I do not have one ActiveRecord Model
for it.
Even the official docs Formtastic
didn't help.
source to share
I figured out how to do it, and here is the answer for those who are 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
source to share