admin管理员组文章数量:1203823
I have a Ruby array like this in my controller:
@location_list = [
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]
And I am catching it like this in my view:
location_list = "<%= @location_list.to_json %>";
But if I do alert(location_list), I get:
[["Mushrooms",3],["Onions",1],["Olives",1],["Zucchini",1],["Pepperoni",2]]
How do I get the correspondent object without those "?
I have a Ruby array like this in my controller:
@location_list = [
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]
And I am catching it like this in my view:
location_list = "<%= @location_list.to_json %>";
But if I do alert(location_list), I get:
[["Mushrooms",3],["Onions",1],["Olives",1],["Zucchini",1],["Pepperoni",2]]
How do I get the correspondent object without those "?
Share Improve this question asked Jun 6, 2012 at 22:48 Hommer SmithHommer Smith 27.9k62 gold badges175 silver badges307 bronze badges2 Answers
Reset to default 27Try:
<%= raw @location_list.as_json %>
Using to_json
will end up rendering a string, with embedded double-quotes, and would need to be JS-escaped. And it would be a string, not an array.
This worked for me:
<%= @location_list.to_s.gsub(''', '') %>
Basically use .to_s
to convert the whole array to a string, then use .gsub(''','')
to remove the quotes by replacing them with nothing.
本文标签: javascriptFrom Ruby array to JS array in Rails 39quote39Stack Overflow
版权声明:本文标题:javascript - From Ruby array to JS array in Rails- 'quote'? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738670197a2105946.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论