0

I'm trying to dynamically draw a Google Map and place a marker on it. In my controller, I assemble an array containing the name of the businesss, latitude, longitude and a z-index.

I have an array in Ruby that is available to my Rails view:

@locations = [["Location One",36.0552,-114.928,1]]

When I try to use that array in some Javascript:

var loc = <%= @locations %>

I see this in view source:

var loc = [[&quot;Location One&quot;,36.0552,-114.928,1]]

In order for the map to display the variable needs to read like:

var loc = [['Location One',36.0552,-114.928,1]]

With the quotes intact around the first element of the array. If I hard code that last line into the Javascript the map renders correctly. It really seems to be the quot; tags that are messing things up.

There will be more than one location on the map so I thought an array of arrays would be the best way to get the needed data into my view.

How can I accomplish this? Thanks.

2 Answers 2

1

Edit Corrected following DNNX's comment.

I think you need to convert it to json (a subset of JavaScript) before rendering:

require "json"
...
var loc = <%= @locations.to_json.html_safe %>
Sign up to request clarification or add additional context in comments.

2 Comments

<%= @locations.to_json %> doesn't work. <%= @locations.to_json.html_safe %> does.
I didn't know about html_safe. Thanks guys.
0

you need to use html_safe to prevent the escape the quote

1 Comment

Providing a simple example would be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.