Reputation: 1195
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
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
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