activeadmin_addons
activeadmin_addons copied to clipboard
Hidden elements for Tags input not correct for nested attributes
I have been trying to add the tags input to a form with nested attributes, and the hidden input field generated has the incorrect name property.
# Rails models
class Foo < ApplicationRecord
has_many :bars
accepts_nested_attributes_for :bars
end
class Bar < ApplicationRecord
belongs_to :foo
def tags
serialized_tags&.split(',')
end
def tags=(tags_string)
update_attributes(serialized_tags: tags_string)
end
end
# ActiveAdmin
ActiveAdmin.register Foo do
permit_params :name, bars_attributes: [:id, :name, tags: []]
# index ...
# show ...
form do |f|
f.inputs 'Foo details' do
f.input :name
end
f.has_many :bars, allow_destroy: false, new_record: true do |b|
b.inputs b.object.name do
b.input :name
b.input :tags, as: :tags, collection: ['bar1', 'bar2', 'bar3']
end
end
f.actions
end
end
The select for this looks ok
<select id="foo_bars_attributes_0_tags" class="tags-input select2-hidden-accessible" data-model="bar" data-method="tags" data-width="80%" data-collection="[<snip>]" name="foo[bars_attributes][0][virtual_tags_attr]" tabindex="-1" aria-hidden="true" multiple="" data-select2-id="foo_bars_attributes_0_tags">
<option value="bar1" data-select2-id="3">bar1</option>
<option value="bar2" data-select2-id="4">bar2</option>
<option value="bar3" data-select2-id="5">bar3</option>
</select>
but the hidden input generated by the above looks like this:
<input id="bar_tags" name="bar[tags]" type="hidden">
The resulting params are incorrect
{
"_method": "patch",
"authenticity_token": "...",
"foo": {
"name": "Foo",
"bars_attributes": {
"0": {
"name": "Bar 1",
"virtual_tags_attr": "bar1",
"id": "1"
}
}
},
"bar": {
"tags": "bar1"
},
"commit": "Update Foo",
"controller": "admin/foos",
"action": "update",
"id": "1"
}
Edit: Fixed saving and splitting serialized tags in Bar
and :name
to :bars_attributes
in permit_params
Hi @jhofmeyr! Sorry for the delayed answer. I'm not sure from reading the example provided if I understand correctly the issue, as I don't fully get which params you would expect for it to be working correctly.
I'd be glad to try and help if I'm able to understand a bit more haha!
Hi @rjherrera, thanks for the response! It's been a while since I looked at this, but if I remember correctly the issue is basically that the data collected by the :tags
input does not get saved to nested objects. The issue appears to be with the hidden input that gets generated. In particular, I believe these should be name="foo[bars_attributes][0][tags]"
- editing the HTML in my browser to this value correctly saves the results.
It may just be that I was just attempting to using the input incorrectly, if so maybe the docs could be updated with the correct usage for nested attributes?