I have a Tag model as a polymorphic table that I'm using on two models Account and Activity. I would like to replace accounts and activity by their names when choosing them in the active admin form.
The form is working when accounts or activities are displayed as instances. But when I try to replace them by their names (see code below), I get the error "must exist and can't be blank" in the form. I've browsed the documentation of ActiveAdmin but could not find my answer.
ActiveAdmin.register Tag do
# belongs_to :spot
permit_params :name, :taggable_type, :taggable_id
form do |f|
f.semantic_errors
f.inputs do
f.input :name,
collection: Tag::TAG_NAMES
f.input :taggable_type,
collection: Tag::TAGGABLE_TYPES
f.input :taggable, label: "Account / Activity",
collection: Account.all.map { |a| a.nickname } +
Activity.all.map { |act| act.name }
end
f.actions
end
end
I would like to have something like label_method and value_method that would allow me to display accounts and activities with their names instead of instances, which is not user-friendly at all, but still save them as instances without getting the mentioned error.
Any help is greatly appreciated, thanks :)
You need to pass id as option value. Try
f.input :taggable_id, label: "Account / Activity",
collection: Account.all.map { |a| [a.nickname, a.id] } +
Activity.all.map { |act| [act.name, act.id] }