I've created a rails action that makes a GET request to a service and then passes the response to the view as @job_data. The code works fine in retrieving the response, and I've verified the json 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'm trying to do a little javascript to manipulate DOM elements using this json object. I'm trying to insert the script into the html to do something very basic with the json object, but can't seem to get it to pass correctly:
<script type="text/javascript" charset="utf-8">
$(function() {
var $data = <%= @job_data %>;
$('#jobtable').append($data.Id);
When I debug the page I see that all the entities in the string are encoded (e.g. " instead of quotation marks). I've tried to de-entityify the string with HTMLEntities as in the code above for the action, but it has no effect and is the same as if I had left the @job_data set to:
@job_data = res.body
Can anyone explain where the encoding happens and how I can resolve this? Is the problem with the action or with the javascript in the view? What's the preferred way to pass a json string/object from a response in an action to javascript in the view?