I have some values in ruby (variables and objects / hash) that I want to pass on to javascript on the rendered page. Currently I use these methods to simply write the javascript of declaring the variables at the client end.
def declare_as_js_vars vars
string = ""
vars.each do |name, value|
string += self.declare_as_js_var(name, value)
end
string
end
def declare_as_js_var name, value
name.to_s + "='" + value.to_s + "';"
end
The problem here is that I am unable to declare objects, and have to declare vars individually. I was wondering if there is some way in rails to easily do this because this is turning out to be quite a hack.
How should I pass variables and objects to javascript? Please provide some syntax example