ruby - JSON with Rails -
someone please this!!!
i'm trying parse json object in javascript within rails, , nothing seems work.
the story in controller is:
def map @nodes = node.all @json = {"nodes" => @nodes.as_json(:only => [:id, :lat, :lon])} end
in view, have simple javascript:
<script type="text/javascript"> var stuff = <%= @json %>; var json = json.parse(stuff); alert("text"); </script>
i'm trying see if code runs through first 2 lines alert message, never works, throwing me unexpected token error (usually colon or { ). i've tried doing eval method, doesn't work. can please me parse json in javascript? eternally grateful....
you can go current approach, there few correction need make work
@json not json here, hash , when assign javascript variable looks like
stuff = {"nodes" => node value}
here => not accepted in javascript, not valid json object.
you need convert hash object in json like
var stuff = <%= @json.to_json %>;
Comments
Post a Comment