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

Forms nested multiple levels #76

Open
karianne opened this issue Nov 21, 2023 · 4 comments
Open

Forms nested multiple levels #76

karianne opened this issue Nov 21, 2023 · 4 comments

Comments

@karianne
Copy link

karianne commented Nov 21, 2023

I'm trying to use this gem to add a two-level structure: Theme --(has many)--> ThemeSection --(has_many)--> ThemeSectionItem. However, it seems that I'm running into a bug where " is not encoded correctly, since I get this html when trying to add a ThemeSection:

<div id="all_sections">
              <div class="section_container added-by-vanilla-nested">
  <p class="form-group form-field string required theme_theme_sections_title"><label class="control-label form-field__label string required" for="theme_theme_sections_attributes_1700576891368_title"><abbr title="påkrevd">*</abbr> Tittel</label><span class="form-field__control-wrapper"><input class="form-control form-field__control string required" required="required" aria-required="true" type="text" name="theme[theme_sections_attributes][1700576891368][title]" id="theme_theme_sections_attributes_1700576891368_title"></span></p>
  <input value="0" autocomplete="off" type="hidden" name="theme[theme_sections_attributes][1700576891368][_destroy]" id="theme_theme_sections_attributes_1700576891368__destroy"><a class=" vanilla-nested-remove " data-undo-text="Undo" data-undo-link-classes="">Remove theme section</a>
  <div class="all_items">
      </div>
  <a class=" vanilla-nested-add " data-container-selector=".all_items" data-html="<div id=" item_container"="">
  <p class="form-group form-field integer optional theme_theme_sections_theme_section_items_itemable_id"><label class="control-label form-field__label integer optional" for="theme_theme_sections_attributes_1700576891368_theme_section_items_attributes_1700576891368_itemable_id">Itemable</label><span class="form-field__control-wrapper"><input class="form-control form-field__control numeric integer optional" type="number" step="1" name="theme[theme_sections_attributes][1700576891368][theme_section_items_attributes][1700576891368][itemable_id]" id="theme_theme_sections_attributes_1700576891368_theme_section_items_attributes_1700576891368_itemable_id"></span></p>
  <input value="0" autocomplete="off" type="hidden" name="theme[theme_sections_attributes][1700576891368][theme_section_items_attributes][1700576891368][_destroy]" id="theme_theme_sections_attributes_1700576891368_theme_section_items_attributes_1700576891368__destroy"></a><a class=" vanilla-nested-remove " data-undo-text="Undo" data-undo-link-classes="">Fjern denne artikkelen</a>
</div>" data-method-for-insert="append"&gt;Add ThemeSectionItem
</div>

(note the broken data-method-for-insert at the bottom, which should have been a part of the div added by vanilla-nested)

Is this gem even supposed to be used with two levels?


For reference, here's my form ERB:

_form.html.erb:

<%= simple_form_for(theme) do |f| %>
....
<%= field_set_tag 'Sections' do %>
      <%= link_to_add_nested(f, :theme_sections, '#all_sections') %>
      <div id="all_sections">
        <%= f.simple_fields_for :theme_sections do |sf| %>
          <%= render 'theme_section_fields', form: sf %>
        <% end %>
      </div>
    <% end %>
    <%= f.button :submit, class: 'button--standalone' %>
<% end %>

theme_section_fields.html.erb:

<div class="section_container">
  <%= form.input :title %>
  <%= link_to_remove_nested(form, link_text: "Remove theme section") %>
  <div class="all_items">
    <%= form.simple_fields_for :theme_section_items do |sif| %>
      <%= render 'theme_section_item_fields', form: sif %>
    <% end %>
  </div>
  <%= link_to_add_nested(form, :theme_section_items, '.all_items') %>
</div>

theme_section_item_fields.html.erb:

<div id='item_container'>
  <%= form.input :itemable_id %>
  <%= link_to_remove_nested(form, link_text: "Remove item") %>
</div>
@codeborgar
Copy link

Hi, I think I have seen your issue on another gem as well, I am having the same problem in general. Have you managed to fix or find a gem that does this nicely?

@arielj
Copy link
Owner

arielj commented Dec 7, 2023

hey! sorry I missed the original message

I'll check today, I have some idea on how to fix it, will keep you posted

@arielj
Copy link
Owner

arielj commented Dec 7, 2023

@karianne @codeborgar can you try this branch? #77

if you are using just the ruby gem you can change the gemfile to do:

gem 'vanilla_nested', github: 'arielj/vanilla-nested', branch: "support-3-level-nesting"

if you are also using the yarn package, you can change with:

yarn add arielj/vanilla-nested#support-3-level-nesting

Note that nesting is difficult to handle automatically so there are some notes of a naming convention that's needed in this case explained on the PR description (hopefully it's clear enough, but feel free to ask here, that will help me improve the README.

@viktormarkov
Copy link

@arielj Hi, thank you for supporting the gem!
I've tried the solution from PR #77, and it seems to be working as described.
Can we expect this feature to be merged for the next release?

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

No branches or pull requests

4 participants