Colibri
Colibri

Reputation: 1195

SyntaxError: Unexpected token '&'. Expected a property name

I'm using Slim. When I describe a javascript: block, I use this inside the JS code:

javascript:
    var tteesstt = #{users.select(:id, :email).order(:email).to_json};

But I get this error:

SyntaxError: Unexpected token '&'. Expected a property name.

I tried to wrap in JSON.parse(), but it did not help.

Tell me, please, how to solve the problem?

Upvotes: 1

Views: 1634

Answers (2)

Arup Rakshit
Arup Rakshit

Reputation: 118289

In Slim, we can do assign JSON data as JS object using {{}}.

javascript:
  var tteesstt = #{{users.select(:id, :email).order(:email).to_json}};
  console.log(tteesstt); // ensures it gets the data as expected.

Upvotes: 1

Jagdeep Singh
Jagdeep Singh

Reputation: 4920

Your JSON string must have some special characters which are causing the error. You can use raw to fix it:

javascript:
  var tteesstt = #{raw users.select(:id, :email).order(:email).to_json};

From the definition:

This method outputs without escaping a string. Since escaping tags is now default, this can be used when you don’t want Rails to automatically escape tags.

Upvotes: 1

Related Questions