Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Cocoon (Nested Forms) and jQuery sortable together
    primarykey
    data
    text
    <p>I currently have an input form generated using Cocoon and would like to make the input fields created by it sortable (using sortable from jQuery-ui).</p> <p>The code for the nested form is: </p> <pre><code>&lt;%= f.simple_fields_for :checkout_procedures do |procedure| %&gt; &lt;li&gt;&lt;%= render 'checkout_procedure_fields', :f =&gt; procedure %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;div class="links"&gt; &lt;%= link_to_add_association 'add step', f, :checkout_procedures %&gt; &lt;/div&gt; </code></pre> <p>The partial that is rendered (_checkout_procedure_fields) is: </p> <pre><code>&lt;div class="checkout_procedure nested-fields"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;%= f.input :step %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to_remove_association "remove step", f %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; </code></pre> <p>I am able to get the existing elements sortable by putting them inside of a <code>&lt;div&gt;</code> and setting it sortable using jQuery:</p> <pre><code>&lt;div class="sortThese"&gt; &lt;%= f.simple_fields_for :checkout_procedures do |procedure| %&gt; &lt;li&gt;&lt;%= render 'checkout_procedure_fields', :f =&gt; procedure %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/div&gt; &lt;div class="links"&gt; &lt;%= link_to_add_association 'add step', f, :checkout_procedures %&gt; &lt;/div&gt; &lt;script&gt; $(document).ready(function() { $(".sortThese").sortable(); }); &lt;/script&gt; </code></pre> <p>But doing this breaks the functionality of Cocoon's <code>link_to_add_association</code> and <code>link_to_remove_association</code>. Using <code>&lt;ul&gt;</code> with <code>&lt;li&gt;</code> also results in the same Cocoon malfunction. I've been looking around to hack some javascript that moves each input form in and out of the sortable div (doing this seems to make the links work again), but obviously this is inelegant. If anybody has any suggestions, I'd really appreciate them!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload