Hommer Smith
Hommer Smith

Reputation: 27852

Rails + JS - Using instance variable (array of arrays with strings)

I have the following in a html.erb file:

<%= @location_list = [['test',2]] %>
<script type="text/javascript">
 var test = <%= @location_list.to_json %>
 alert(test);
</script>

And the alert is not showing up.

However if I do <%= @location_list = [[3,2]] %> - The alert is showing up.

Why?

Upvotes: 1

Views: 333

Answers (1)

Ben Zhang
Ben Zhang

Reputation: 1281

<%= %> tag means output something. In your case, you probably dont want to output anything, so I guess you are looking for <% @location_list = [['test',2]] %>, which means normal statement no output will be involved

Sorry, havn't really answer your question.

var test = <%= @location_list.to_json %>

should be

var test = "<%=j @location_list.to_json %>"

Upvotes: 1

Related Questions