Roberto Pezzali
Roberto Pezzali

Reputation: 2504

JQuery, Rails and Json: Unexpected token o

I'm a beginner with a good ruby & rails knowloedge but a very limited javascript knowledge.

I'm trying to push some values in a Select2 jquery plugin instance.

I'm using this script to manage my Select:

$('.ajaxselect').each(function() {
    var url = $(this).data('url'); 
    var placeholder = $(this).data('placeholder'); 
    var saved = jQuery.parseJSON($(this).data('saved'));
    $(this).select2({
      minimumInputLength: 2,
      multiple: true,
      placeholder : placeholder,
      allowClear: true,
      ajax: {
        url: url,
        dataType: 'json',
        quietMillis: 500,
        data: function (term) {
          return {
            name: term
          };
        },
        results: function (data) {
          return {results: data};
        }
      },

      formatResult: function (item, page) {
        return item.name; 
      },

      formatSelection: function (item, page) {
        return item.name; 
      },

      initSelection : function (element, callback) {
        if (saved) {
          callback(saved);
        }
      }

    });
  });

I have a problem with "Unexpected token o" on this line:

var saved = jQuery.parseJSON($(this).data('saved'));

In my view I'm calling this code in this way:

= form.input :brand_ids, label: false,          |
                  input_html:                                     |
                  {                                               |
                    class: 'ajaxselect form-control',             |
                    data: {                                       |
                      placeholder: "Scegli brand",                |
                      saved: content.brands.to_json,              |
                      url: '/admin/brands/list.json'              | 
                           },                                     |                       
                  }    

My saved value generated in the right way: I try with pry and content.brands.to_json return me the rights data.

Here is my binding.pry log

pry(#<#<Class:0x007fc66caecd30>>)> content.brands.to_json
=> "[{\"id\":1,\"name\":\"Sony\"},{\"id\":2,\"name\":\"Panasonic\"}]"

Here is my HTML generated code

<input class="string optional form-control form-control form-control form-control ajaxselect form-control" data-placeholder="Scegli brand" data-saved="[{&quot;id&quot;:1,&quot;name&quot;:&quot;Sony&quot;},{&quot;id&quot;:2,&quot;name&quot;:&quot;Panasonic&quot;}]" data-url="/admin/brands/list.json" id="content_brand_ids" name="content[brand_ids]" type="text" value="[1, 2]">

Why I'm having this problem? to_json is a method provided by rails...

Upvotes: 0

Views: 312

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388446

The problem is with the line

var saved = jQuery.parseJSON($(this).data('saved'));

it should be

var saved = $(this).data('saved');

because .data() will do the parsing for you

Demo: Problem, Solution

Upvotes: 1

Related Questions