I\'m trying to get my JSON from my controller to my view. In my controller I am doing:
@nodes = Node.all
@json = @nodes.as_json(:only => [:ID, :Lat, :Lon])
<
I think you need to put quotes around it, then you can ask jquery to parse the string into JSON.
This is Rails html-encoding your string as is default in Rails 3.
You need to mark your JSON as html_safe
:
var stuff = <%= @json.to_s.html_safe %>
Note that .to_s
is needed because as_json
gives Hash instead of string. You could do this instead:
# in controller
@json = @nodes.to_json(:only => [:ID, :Lat, :Lon])
#and in view
var stuff = <%= @json.html_safe %>