Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hidden elements for Tags input not correct for nested attributes #324

Open
jhofmeyr opened this issue Oct 14, 2020 · 2 comments
Open

Hidden elements for Tags input not correct for nested attributes #324

jhofmeyr opened this issue Oct 14, 2020 · 2 comments
Assignees
Labels

Comments

@jhofmeyr
Copy link

jhofmeyr commented Oct 14, 2020

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

@rjherrera
Copy link
Member

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!

@jhofmeyr
Copy link
Author

jhofmeyr commented Jan 26, 2021

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?

@ldlsegovia ldlsegovia self-assigned this Jul 29, 2021
@ldlsegovia ldlsegovia added the bug label Jul 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants