You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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
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.
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
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,
The first select is correctly populated but the second select is empty with error displayed in the caption
No records found
The text was updated successfully, but these errors were encountered: