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

nested_select not working on forms for has_many :through relationships #350

Open
erotenberg1 opened this issue Jun 1, 2021 · 1 comment

Comments

@erotenberg1
Copy link

erotenberg1 commented Jun 1, 2021

Expected Behavior

I want to create a form for a has_many :through relationship. Using f.has_many method in such a form should allow me to multiple associated records with :nested_select elements.

Actual Behavior
The nested element is not populated, the select box is empty with caption "No results found"

Example
I have model Category with has_many :tags and model Tag with belongs_to :category.

I have model ReportTag that belongs_to :tag and :report

When I make a form in ReportTag's resource page

ActiveAdmin.register ReportTag, namespace: :u do
  permit_params :tag_id, :id, :report_id

  form do |g|
    g.inputs "ReportTag" do
      g.input :report
      g.input :tag_id, as: :nested_select, 
      level_1: {attribute: :category_id, collection: Category.all, display_name: :name}, 
      level_2: {attribute: :tag_id, collection: Tag.all, display_name: :value}
    end
    g.actions
  end
end

I get the correct behavior, the second select field correctly populates depending on category_id selected.

However, when I try to implement this in the resource page for Report which has_many :report_tags,

ActiveAdmin.register Report, namespace: :u do
  permit_params :user_id, :name, :description, :start_year, :end_year,
       report_tags_attributes: [:id, :tag_id, :report_id, :_destroy]

  form do |f|
    f.resource.user=current_user
    f.semantic_errors
    f.inputs "Tag" do
      f.input :user_id, as: :hidden
      f.input :name
      f.input :description
      f.input :start_year
      f.input :end_year
      f.inputs "Report Tags" do
        f.has_many :report_tags, allow_destroy: true, new_record: "add tag" do |g|
          g.input :tag_id, as: :nested_select, 
            level_1: {attribute: :category_id, collection: Category.all, display_name: :name}, 
            level_2: {attribute: :tag_id, collection: Tag.all, display_name: :value}
        end
      end
    end
    f.actions
  end
end

The first select is correctly populated but the second select is empty with error displayed in the caption No records found

@erotenberg1 erotenberg1 changed the title nested_select works not working on has_many nested_select not working on forms for has_many :through relationships Jun 1, 2021
@rjherrera
Copy link
Member

hi there @erotenberg1! I stumbled upon this behavior yesterday while trying to implement a similar form in AA. The same happened to me and I thought it had to do with an AA Addons bug, however, in my case it had to do with the permit_params method.

At first I had not included the equivalent to your tag_id parameter in my array and once I added it it started working. In my particular case I also had to also add the equivalent to your category_id, so maybe that helps.

Regardless of that, I still think it is a weird behavior. In my opinion, a problem related to not including something in the permit_params method call should be noticed when actually using the save button and not seeing any changes reflected, but not in the way the input behaves before actually committing changes.

Also, I tried with an older version (1.7.1) and it didn't work, even after adding the parameters to the permit params call, but it did work in the latest (1.8.3), so I would suggest trying the update.

I hope this helps, I know it's incredibly late, sorry about that 🙏🏻 , but we were not able to reproduce it till now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants