1

In a rails app I have an object defined in a controller containing an icon and some values:

@data = "<i class='fa fa-circle fa-lg'></i><span>Speed: </span> #{b.speed.to_f} | Time: #{b.gps_date.try(:strftime, '%-m/%e %H:%M')}} <br />".html_safe.to_json

In view I parse it like this:

<script>
  var data = JSON.parse('<%= raw @data.as_json %>');
</script>

But I get the following error:

Uncaught SyntaxError: missing ) after argument list

I works fine when I remove the icon code that contains the single quotes for class

<i class='fa fa-circle fa-lg'></i>

How can I fix this error?

5
  • Not a solution but just curious to know, why are you using to_json and as_json ? Commented Oct 21, 2016 at 11:14
  • I get the same error if I don't use these methods. I'm actually using this object in JavaScript so tried to_json and then parsed the json in JS Commented Oct 21, 2016 at 11:30
  • 2
    This does not look like a object. I guess you are trying to build a html node and pass it to JS then I guess only '<%= raw @data %>' should work. Have you tried ? Commented Oct 21, 2016 at 11:32
  • You're right. Removed all json parsing code and used just raw @data. It worked. Thanks for the help Commented Oct 21, 2016 at 11:38
  • I will post this as answer, if you want then you can accept it. Commented Oct 21, 2016 at 11:39

2 Answers 2

2

You want to send the HTML string to JS so need of to_json, as this is used to convert Hash to JSON . So just use html_safe in server side.

And in client side, since you have the all HTML in string no need of as_json, just use the string as you would normally do in JS. as_json is a method used as ActiveRecord Serializer.

@data = "<i class='fa fa-circle fa-lg'></i><span>Speed: </span> #{b.speed.to_f} | Time: #{b.gps_date.try(:strftime, '%-m/%e %H:%M')}} <br />".html_safe

and

var data = <%= raw @data %>;
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I meant that. No need to parse as JSON since thats is a string. Forgot to remove while posting :)
1

Basically this seems to be an issue with an unescaped ' popping up somewhere in JSON.parse('...'). You can verify the issue by looking at the HTML of the rendered page.

I think you might fix your issue by declaring (no need for the .to_json here):

@data = "<i class='fa fa-circle fa-lg'></i><span>Speed: </span> #{b.speed.to_f} | Time: #{b.gps_date.try(:strftime, '%-m/%e %H:%M')}} <br />".html_safe

And then in the view use

var data = "<%= escape_javascript @data %>";

as you are only tring to pass a string there is no need for the conversion into a JSON object and then parsing it back. (You'd need that if you wanted to pass a whole Javescript Object/Ruby Hash).

Actually for the last line there is also an equivalent shortcut/alias:

var data = "<%= j @data %>";

PS: I described how to fix the imminent issue but generally the point of MVC is to not have (or at least to minimize) presentation logic in a conroller. Generally it would be preferable to pass the data to the view and generate the HTML in the template.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.