javascript - Why is my embedded ruby json string encoded with html entities when parsed in the view? -
I have taken a rail operation that requests a GET for the service and then replies to the answer @job_data. The code works fine in retrieving the response, and I've verified that the Jason object looks correct.
def show @ url = URI.parse (@@ jobs_base_url + "/" + params [: ID] + "? TestMode = true") req = Net :: HTTP :: Get New (@ url.to_s) res = Net :: HTTP.start (@ url.host, @ url.port) {| Http | Http: request (req)} @job_data = HTMLEntities.new.decode res.body end
However, I have to javascript a bit to manipulate DOM elements using this json object I am trying. I am trying to incorporate the script into HTML to do something basic with the JSN object, but it may not seem to be correctly passed:
& lt; Script type = "text / javascript" charset = "UTF-8" & gt; $ (Function () {var $ data = & lt;% = @ job_data%> $ ('# jobable') .andend ($ data.Id);
when I debug I see that all the entities in the string are encoded (for example, instead of quotation marks). I have tried to put the string in the code above with the HTMLNTTs in the above code, but it has no effect and it Just like I had left the @job_data set:
@job_data = res.body
What E can tell when the encoding happens and how can I solve it? Is there a problem in action or is it in javascript view? What is the preferred way of passing Jason String / Object in an action for javascript in the scene?
Already answered:
Use of the trick < Code> raw is done in embedded view by encoding Ruby said:
var $ data = & lt;% = raw @ job_data% & gt ;;
Comments
Post a Comment