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

Multiple selected_lists in nested association loop act as single select #410

Open
rg34 opened this issue Jun 30, 2022 · 0 comments
Open

Comments

@rg34
Copy link

rg34 commented Jun 30, 2022

Hello. Thank you so much for a GREAT library that I've used a lot! You have saved me a lot of time.

I have an application using ruby 3.1.0, rails 6.1.5 and ActiveAdmin 2.11.1, ActiveAdmin Addons 1.9.0.
I am trying to loop through a nested association in a form and one of the inputs needed is a selected_list.
Everything renders perfectly, however, when I make a selection in one dropdown, it populates every selected_list on the form. I've removed the other fields in the form to isolate the problem:

duplicate_selected

the relationship is:
Application has_many :submissions
Submission has_many :decisions
Decisions HABTM Reasons => reasons_decisions join table

@submission = application.submissions.last:

f.semantic_fields_for @submission do |fs|
  fs.semantic_fields_for :decisions do |r|
    r.inputs do
      ...
       r.input :reason_ids, as: :selected_list, label: 'Reasons', display_name: :explanation, fields: [:explanation],
              minimum_input_length: 0, url: admin_submission_reasons_url
     end
  end
end

Resulting in the following html (when there are two decisions, for example):

<fieldset class="inputs">
   <ol>
      <li class="selected_list input optional stringish" id="application_submission_decisions_attributes_0_reason_ids_input">
         <label for="application_submission_decisions_attributes_0_reason_ids" class="label">Reasons</label>
         <div class="selected-list-container">
            <div id="decision_reason_ids_selected_values" class="selected-values"><input id="decision_reason_ids_empty" name="decision[reason_ids][]" value="" autocomplete="off" type="hidden"></div>
            <select id="application_submission_decisions_attributes_0_reason_ids" class="selected-list-input select2-hidden-accessible" data-model="decision" data-method="reason_ids" data-url="http://localhost:3000/admin/applications/89/submission_reasons" data-response-root="reasons" data-fields="[&quot;explanation&quot;]" data-predicate="contains" data-display-name="explanation" data-minimum-input-length="0" data-width="100%" data-order="explanation_desc" name="application[submission][decisions_attributes][0][virtual_reason_ids_attr]" tabindex="-1" aria-hidden="true" data-select2-id="application_submission_decisions_attributes_0_reason_ids"></select><span class="select2 select2-container select2-container--default" dir="ltr" data-select2-id="9" style="width: 100%;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-disabled="false" aria-labelledby="select2-application_submission_decisions_attributes_0_reason_ids-container"><span class="select2-selection__rendered" id="select2-application_submission_decisions_attributes_0_reason_ids-container" role="textbox" aria-readonly="true"></span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>
         </div>
      </li>
   </ol>
</fieldset>
<input autocomplete="off" type="hidden" value="37" name="application[submission][decisions_attributes][0][id]" id="application_submission_decisions_attributes_0_id">
<fieldset class="inputs">
   <ol>
      <li class="selected_list input optional stringish" id="application_submission_decisions_attributes_1_reason_ids_input">
         <label for="application_submission_decisions_attributes_1_reason_ids" class="label">Reasons</label>
         <div class="selected-list-container">
            <div id="decision_reason_ids_selected_values" class="selected-values"><input id="decision_reason_ids_empty" name="decision[reason_ids][]" value="" autocomplete="off" type="hidden"></div>
            <select id="application_submission_decisions_attributes_1_reason_ids" class="selected-list-input select2-hidden-accessible" data-model="decision" data-method="reason_ids" data-url="http://localhost:3000/admin/applications/89/submission_reasons" data-response-root="reasons" data-fields="[&quot;explanation&quot;]" data-predicate="contains" data-display-name="explanation" data-minimum-input-length="0" data-width="100%" data-order="explanation_desc" name="application[submission][decisions_attributes][1][virtual_reason_ids_attr]" tabindex="-1" aria-hidden="true" data-select2-id="application_submission_decisions_attributes_1_reason_ids"></select><span class="select2 select2-container select2-container--default" dir="ltr" data-select2-id="10" style="width: 100%;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-disabled="false" aria-labelledby="select2-application_submission_decisions_attributes_1_reason_ids-container"><span class="select2-selection__rendered" id="select2-application_submission_decisions_attributes_1_reason_ids-container" role="textbox" aria-readonly="true"></span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>
         </div>
      </li>
   </ol>
</fieldset>
<input autocomplete="off" type="hidden" value="38" name="application[submission][decisions_attributes][1][id]" id="application_submission_decisions_attributes_1_id">

When I click into the search select in one input, I see that a data-select2-id gets added and then, when I click in the other search select, a unique data-select2-id gets added to that one.
data_select_id

And, the actual selects each have a unique id:
application_submission_decisions_attributes_0_reason_ids
application_submission_decisions_attributes_1_reason_ids

I have tried adding additional ids and classes to the inputs for uniqueness.

My permit params:

  permit_params :admin_user_id, :status,
                screenshots: [], active_admin_comments_attributes: %i[body author_id namespace resource_id resource_type author_type],
                submission: [:id, { decisions_attributes: [:id, :grade, reason_ids: []]}]

but something is definitely wonky with the nested params when I submit the form ( "decision"=>{"reason_ids"=>["", "", "60"]},)

{"_method"=>"patch",
 "authenticity_token"=>"[FILTERED]",
 "application"=>
  {"admin_user_id"=>"7",
   "status"=>"new",
   "current_admin_user_id"=>"1",
   "active_admin_comments_attributes"=>{"0"=>{"body"=>"", "author_id"=>"1", "namespace"=>"admin", "author_type"=>"AdminUser", "resource_type"=>"Application", "resource_id"=>"89"}},
   "submission"=>{"decisions_attributes"=>{"0"=>{"id"=>"37"}, "1"=>{"id"=>"38"}}}},
 "decision"=>{"reason_ids"=>["", "", "60"]},
 "commit"=>"Update Application",
 "id"=>"89"}

THANK YOU for any guidance on what I am doing wrong here. It's SO close to working, I don't want to give up!
I've tried with a search_select and do not have the same problem, but I really need the list.

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

1 participant