Take the 2-minute tour ×

I am dealing with Ruby on Rails and JavaScript.
The main basic idea is that I create an object in my model's class function and call this function in JavaScript. The problem there is that anywhere the HTML entities are encoded.

Now in detail:
In my model's function getData(firstOpt, secondOpt, amount) I create an object that looks as follows (when I call the function in the ruby console):

[#<JobCompact build_compact_id: 324>, #<JobCompact build_compact_id: 325>, #<JobCompact build_compact_id: 325>] 

Now I put this into an array (because I want to have more entries in that array later) at the first position:

data[0] = firstLang  

(note: I also tried -> data[0] = firstLang.to_json)

In the end I just return the array data. Now I call the function in JavaScript:

var curData = <%= BuildCompact.getData("ruby", "rubinius", 15) %>;

And the error is: Uncaught SyntaxError: Unexpected token ILLEGAL
because it translated my result of the ruby function to: var curData = [[#&lt;JobCompact id: 841, language: &quot;ruby&quot;, version: &quot;1.609481891837258&quot; etc.

I also tried to decode the result:
var curData = htmlentities((<%= BuildCompact.getData("ruby", "rubinius", 15) %>).toString());

And many other variants of that but I cannot figure it out. Has anybody had a similar problem?

NOTE: I hope that post is NOT a duplicate because I do not know where to encode (in Ruby oder Javascript).

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Ok, the answer to MY question is:

var curData = <%=raw BuildCompact.getData("ruby", "rubinius", 500).to_json %>;
share|improve this answer
add comment

Your Answer


