Anshu
Anshu

Reputation: 41

Jquery tokenInput not prepopulating the text_field

Hi I've tokenInput documentation but somehow my text_field category_tokens is not prepopulated with categories while editing.

Here are code snippets

<input id="product_category_tokens" type="text" size="30" name="product[category_tokens]" data-pre="[{"created_at":"2010-09-13T03:33:17Z","description":"","id":x,"name":"Kitchen & Dining ","parent_id":xx,"permalink":"kitchen-dining","updated_at":"2011-01-05T11:17:10Z"}]" style="display: none;">

$(function() {
  $("#product_category_tokens").tokenInput("/categories.json", {
      crossDomain: false,
      prePopulate: $('#product_category_tokens').data('pre'),
      preventDuplicates: true
  });
});

<%= f.text_field :category_tokens, "data-pre" => @product.categories.map(&:attributes).to_json %>

Any pointer is appreciated!

Upvotes: 0

Views: 1534

Answers (3)

Davy Cardinaal
Davy Cardinaal

Reputation: 11

change

<%= f.text_field :category_tokens, "data-pre" => @product.categories.map(&:attributes).to_json %>

to

<%= f.text_field :category_tokens, input_html => { "data-pre" => @product.categories.map(&:attributes).to_json } %>

Upvotes: 1

Atif
Atif

Reputation: 427

Json should return objects not as string this will convert string to Json object

 JSON.parse(tokendata)

Upvotes: 1

Lucas Moreira
Lucas Moreira

Reputation: 9

Maybe you can try to change your prePopulate to $(this).data("pre").

Another tip is to use field class instead of id in the JS function and in your view since it changes when rendered.

Hope it help you.

Upvotes: 1

Related Questions