Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609678
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:01:23+00:00 2026-05-22T01:01:23+00:00

I’m trying to get my JSON from my controller to my view. In my

  • 0

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]) 

In my view I have tried:

1) var stuff = <%= @json %>
2) var stuff = <%= @json.to_json %>
3) var stuff = <%= @json.to_json.to_json %>

and all of those give me an error. I usually get an "Unexpected Syntax Error &" or "Unexpected Syntax Error {"

I have also tried using jquery and using respond_to within the controller, but that doesn’t seem to work either.

My thoughts are that getting json to the view shouldn’t be a big issue and shouldn’t require jQuery, and currently, my page source looks like:

var stuff = [{&quot;node&quot;:{&quot;ID&quot;:1301499692582,&quot;Lat&quot;:42.3605063113369,&quot;Lon&quot;:-71.0870862191138}},{&quot;node&quot;:{&quot;ID&quot;:1301499691515,&quot;Lat&quot;:42.3605147089149,&quot;Lon&quot;:-71.0870533282532}},{&quot;node&quot;:{&quot;ID&quot;:1301431075499,&quot;Lat&quot;:42.3605456103,&quot;Lon&quot;:-71.0875239075536}} etc

I dont understand the &quot symbols (maybe thats where the syntax error is coming from) but when I do render :json => @nodes.to_json, the page renders a normal json that is valid:

[{"node":{"ID":1301499692582,"Lat":42.3605063113369,"Lon":-71.0870862191138}},{"node":{"ID":1301499691515,"Lat":42.3605147089149,"Lon":-71.0870533282532}},{"node":{"ID":1301431075499,"Lat":42.3605456103,"Lon":-71.0875239075536}}

Note: I’ve also tried doing var stuff = '<%= @json.to_json %> but when I do var json = JSON.parse(stuff), it gives me an illegal token error.

Can someone please help me with this? Thanks so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:01:24+00:00Added an answer on May 22, 2026 at 1:01 am

    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 %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.