I am facing an issue with accessing values of a hash created by a ruby function inside javascript.
This is the code snippet from my controller:
class TransferController < ApplicationController
def index
require 'json'
#@transfers = Transfer.select("transfer_id,section_id,sum(net) as net").group("transfer_id,section_id").having("sum(net) <> ?",0).order("transfer_id ASC")
@transfers = Transfer.select("section_id,section_name,sum(net) as net").group("section_id,section_name").order("section_id ASC")
h = Hash.new()
a = []
@transfers.each do |section|
h["name"] = section.section_name
h["size"] = section.net
a.insert(h)
end
@sectionhash = Hash.new()
@sectionhash["name"] = "stringush"
@sectionhash["children"] = a
end
end
And here is the relevant part from my view:
<h1><%= @sectionhash["name"] %></h1>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script type="text/javascript">
var x = <%= @sectionhash["name"] %>;
alert(x);
</script>
While the HTML correctly shows the value from the hash, the embedded ruby code in javascript seems to be causing issues. I have tested with an alert message before the problematic line and it works fine. I have checked online forums and they confirm that my syntax is correct. Why could this be happening?