我有一个Ruby变量:
@json = [{"lat":37.8690058,"lng":-122.2555342},{"lat":37.8739362,"lng":-122.2653001},{"lat":37.8701101,"lng":-122.2578559}]
当我尝试:
<script type="text/javascript" charset="utf-8">
var test = <%= @json %>
console.log(test);
</script>
Firebug告诉我:
invalid property id
var test = [{"lat":37.869005...707408,"lng":-122.2545767}]
如何成功设置变量?
答案 0 :(得分:3)
似乎您的JSON引号正在被HTML实体替换。尝试使用.html_safe
var test = <%= @json.html_safe %>
如果不能解决,请查看其他帖子,因为我不是红宝石专家,
答案 1 :(得分:1)
假设@json是一个Ruby对象:
<%= javascript_tag do %>
var test = <%= @json.to_json.html_safe %>;
<% end %>