Rails 3 将 rails 数组传递给使用 javascript 数组的 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7195943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 3 passing a rails array to javascript function which uses a javascript array
提问by Xaxum
I am trying to pass a rails array to a javascript function. The function will then use the array values in javascript so I need to keep it an array.
我正在尝试将一个 rails 数组传递给一个 javascript 函数。然后该函数将使用 javascript 中的数组值,因此我需要将其保留为一个数组。
Here is how I am passing my array:
这是我传递数组的方式:
"graphit([<%=@mpg_values.join(",") %>])"
This produces this in the HTML
这会在 HTML 中产生这个
"graphit([#<Mile:0x6ff0cf0>,#<Mile:0x6ff0c18>,#<Mile:0x6ff0b58>])"
Which are not the values, they should be decimal numbers. I assume this is because it is a reference to the array so that I why I am getting the messed up values. If I do something like to_s I see the values but it has other stuff like the table and filed name in there as well.
哪些不是值,它们应该是十进制数。我认为这是因为它是对数组的引用,所以我为什么会得到混乱的值。如果我执行类似 to_s 的操作,我会看到这些值,但其中还有其他内容,例如表和文件名。
Anybody know how I can get this rails array to a javascript function as an array?
有谁知道如何将这个 rails 数组作为数组传递给 javascript 函数?
Thanks in advance for any help.
在此先感谢您的帮助。
采纳答案by Xaxum
Going to JSON as suggested a couple times in this post always gave me something like this.
按照这篇文章中的几次建议使用 JSON 总是给我这样的东西。
[{"mile":{"date":"2011-05-20","mpg":"18.565887006952"}},{"mile":{"date":"2011-06-01","mpg":"18.471164309032"}}]
[{"mile":{"date":"2011-05-20","mpg":"18.565887006952"}},{"mile":{"date":"2011-06-01","mpg" :"18.471164309032"}}]
What I really wanted was just this... [[2011-05-20, 18.56][2011-06-01, 18.47]]
我真正想要的只是这个... [[2011-05-20, 18.56][2011-06-01, 18.47]]
So I handled it with a helper like so.
所以我用这样的帮手处理了它。
def chart_values()
@chart_values = '['
@mpg_values.each do |m|
@chart_values = @chart_values+'['+m.date.to_s+', '+m.mpg.round(2).to_s+'],'
end
@chart_values = @chart_values+']'
end
Then passed chart_values() to the javascript.
然后将 chart_values() 传递给 javascript。
Likely not the best solution but it gave me the desired values in the format I needed.
可能不是最好的解决方案,但它以我需要的格式为我提供了所需的值。
回答by bokor
Since this still seems like an issue and nobody resolved it. Here goes
因为这似乎仍然是一个问题,没有人解决它。开始
var js_array = [<%= raw @object.to_json %>];
回答by Joshua Pinter
Use Ruby's collect
Method.
使用 Ruby 的collect
方法。
A cleaner version of your solution, Xaxum, would be something like this:
您的解决方案 Xaxum 的更简洁版本将是这样的:
def chart_values
@mpg_values.collect do |mpg_value|
[ mpg_value.date.to_s, mpg_value.mpg.round(2).to_f ]
end
end
The collect
method automatically generates an array of whatever the block results in, so you'll end up with something like this:
该collect
方法会自动生成一个包含块结果的数组,因此您最终会得到如下结果:
[
[2011-05-20, 18.56],
[2011-06-01, 18.47]
]
I also changed the mpg
value to use to_f
, which provide a number instead of a string.
我还将mpg
值更改为 use to_f
,它提供一个数字而不是一个字符串。
回答by Tim B.
If you're using HAML, you can just do this:
如果您使用的是 HAML,您可以这样做:
var js_array = #{object};
var js_array = #{object};
回答by SMAG
bokor's answer works, but causes some linter and IDE errors if you want the array to be saved to a const
, instead of a var
/ let
.
bokor的答案有效,但如果您希望将数组保存到 aconst
而不是var
/ ,则会导致一些 linter 和 IDE 错误let
。
The following variation seems to work and be compliant with the linters and IDEs that I am using.
以下变体似乎有效并且符合我正在使用的短绒和 IDE。
const js_array = JSON.parse('<%= raw @object %>');
const js_array = JSON.parse('<%= raw @object %>');