Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何在erb模板中显示阵列?_Ruby_Erb - Fatal编程技术网

Ruby 如何在erb模板中显示阵列?

Ruby 如何在erb模板中显示阵列?,ruby,erb,Ruby,Erb,我有这个散列,它是我的管理系统查询的结果 a = { "total" => 3310, "subtotal" => 2, "page" => 1, "per_page" => 20, "search" => "fact = ipaddress and host ~ test.com", "sort" => { "by" => nil, "order" => nil }, "results" => { "ho

我有这个散列,它是我的管理系统查询的结果

a = {
  "total" => 3310,
  "subtotal" => 2,
  "page" => 1,
  "per_page" => 20,
  "search" => "fact = ipaddress and host ~ test.com",
  "sort" => { "by" => nil, "order" => nil },
  "results" => {
    "host1.test.com" => { "ipaddress" => "192.168.253.240" },
    "host2.test.com" => { "ipaddress" => "192.168.253.253" }
  }
}
我想在新数组中存储主机的IP地址池,并用模板erb文件显示新数组的值。 像这样的事情:

host1.test.com 192.168.253.240
host2.test.com 192.168.253.253

请求的结果可以是具有不同名称的不同数量的主机。此哈希就是一个示例。

您可以将其分配给变量-
@ip\u地址

@ip_addresses = a['results']
然后在模板文件中使用它

<% @ip_addresses.each do |host, info| %>
  <%= host %> - <%= info['ipaddress'] %>
  ...
<% end %>

- 
...

您可以将其分配给变量-
@ip\u地址

@ip_addresses = a['results']
然后在模板文件中使用它

<% @ip_addresses.each do |host, info| %>
  <%= host %> - <%= info['ipaddress'] %>
  ...
<% end %>

- 
...

放置一个[“结果”]。映射{k,v{k,v[“ipaddress”]]。加入(“”)}
sagarpandya82,谢谢!这是一个非常有用的变量
放置一个[“results”].map{k,v |[k,v[“ipaddress”]].join(“”)}
sagarpandya82,谢谢!这是一个有用的变体