Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery Tokeninput & Dynamic Nested Forms
    primarykey
    data
    text
    <p>I am using the setup of the <a href="http://railscasts.com/episodes/197-nested-model-form-part-2" rel="nofollow">R.B. railcast - #197 Nested Model Form Part 2</a> to dynamically add fields to the form but i am having issues getting the <a href="http://loopj.com/jquery-tokeninput/" rel="nofollow">Tokeninput fields</a> to work.</p> <p>Form:</p> <pre><code>&lt;%= form_for(current_user, :url =&gt; user_products_path) do |f| %&gt; &lt;%= f.error_messages %&gt; &lt;%= f.fields_for :user_products do |builder| %&gt; &lt;%= render "user_product_fields", :f =&gt; builder %&gt; &lt;% end %&gt; &lt;%= link_to_add_fields "Add a UserProduct", f, :user_products %&gt; &lt;%= f.submit "Create" %&gt; &lt;% end %&gt; </code></pre> <p>This is my user_product_fields partial, the token text_fields are what I'm having the issue with:</p> <pre><code>&lt;div class="fields"&gt; &lt;%= f.label :product_token, "Product" %&gt; &lt;%= f.text_field :product_token, :id =&gt; 'product_token' %&gt; &lt;%= f.label :address_token, "Address" %&gt; &lt;%= f.text_field :address_token, :id =&gt; 'address_token' %&gt; &lt;%= f.label :price %&gt; &lt;%= f.text_field :price %&gt; &lt;%= link_to_remove_fields "remove", f %&gt; &lt;/div&gt; </code></pre> <p>Jquery Tokeninput functions inside of my application.js:</p> <pre><code>$(function() { $("#product_token").tokenInput("/products.json", { prePopulate: $("#product_token").data("pre"), tokenLimit: 1 }); }); $(function() { $("#address_token").tokenInput("/business_addresses.json", { prePopulate: $("#address_token").data("pre"), tokenLimit: 1 }); }); </code></pre> <p>What the nested form does in the function is this:</p> <pre><code>function add_fields(link, association, content) { var new_id = new Date().getTime(); var regexp = new RegExp("new_" + association, "g") $(link).parent().before(content.replace(regexp, new_id)); } function remove_fields(link) { $(link).prev("input[type=hidden]").val("1"); $(link).closest(".fields").hide(); } </code></pre> <p>This line here:</p> <pre><code>var new_id = new Date().getTime(); </code></pre> <p>Makes the tokeninput fields dynamic, this is what i pulled up from the HTML, notice the changing long numbers in the fields. This is because of the line above.</p> <pre><code>&lt;label for="user_user_products_attributes_1313593151076_product_token"&gt;Product&lt;/label&gt; &lt;label for="user_user_products_attributes_1313593146876_product_token"&gt;Product&lt;/label&gt; &lt;label for="user_user_products_attributes_1313593146180_product_token"&gt;Product&lt;/label&gt; </code></pre> <p>How can i get my token fields to work when the fields keep changing up?</p> <p>Thank you.</p> <hr> <p><strong>EDIT: New working code.</strong></p> <pre><code> function add_fields(link, association, content) { var new_id = new Date().getTime(); var regexp = new RegExp("new_" + association, "g") $(content.replace(regexp, new_id)).insertBefore($(link).parent()).trigger("nestedForm:added"); } $('div.fields').live("nestedForm:added", function() { $("#product_token", $(this)).tokenInput("/products.json", { prePopulate: $("#product_token", $(this)).data("pre"), tokenLimit: 1 }); }); </code></pre> <hr> <p>When trying to <code>data-pre</code> with TokenInput:</p> <pre><code>def new @user_product = current_user.user_products.build # This line below is for TokenInput to work, This allowed me to use @products.map on the form. @products = [] end def edit @user_product = UserProduct.find(params[:id]) # This line below had to find the Product associated with the UserProduct @products = [@user_product.product] end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    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