Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formtastic/ActiveAdmin set a default value for a select input

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.

like image 317
Morad Edwar Avatar asked Jul 09 '17 12:07

Morad Edwar


1 Answers

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
like image 54
Morad Edwar Avatar answered Sep 24 '22 06:09

Morad Edwar