从hash中取出值并插入ruby中的数组中

时间:2015-07-10 11:15:49

标签: ruby-on-rails ruby

我有

students = {1=>"alexandar", 2=>"mona", 3=>"gaga", 4=>"", 5=>"tom", 35=>"abcd", 44=>"hin"}
array = []

我已经给了这个

array = students.values

当我写下我的test.js.erb

$('.show-test').text('<%= array%>')

它给出了o / p

[&quot;alexandar&quot;, &quot;mona&quot;, &quot;gaga&quot;,
&quot;tom&quot;, &quot;abcd&quot;, &quot;hin&quot;]

现在我需要显示这样的文字

 [alexander, mona, gaga, tom, abcd, hin]

请指导我如何解决这个问题。提前致谢

1 个答案:

答案 0 :(得分:5)

超级简单,请使用Hash#values

$> students = {1=>"alexandar", 2=>"mona", 3=>"gaga", 4=>"", 5=>..............}
$> students.values # <- Array with values
=> ["alexandar", "mona", "gaga", "", "tom", "abcd", "hin"]

更新

如果您尝试从控制器渲染json,请使用html_safe

$('.show-test').text('<%= array.html_safe %>') 

或:

$('.show-test').text('<%= array.to_json.html_safe %>')

或(与html_safe相同):

$('.show-test').text('<%= raw(array.to_json) %>')