jruby问题迭代多维哈希对象

时间:2011-12-08 21:48:26

标签: ruby-on-rails loops jruby

因此,对于rails来说,我似乎很难在循环中创建一个循环来处理信息。

我得到了:

  

无法将符号转换为整数行#11

有问题的一行是:

Host <%= servicedetails[:hostidn] %> - <%= servicedetails[:status] %>

以下是完整版。作为新人我无能为力,愿意接受建议。

<div>
<% @service_hash[:service_list].each do |servicesinfo| %>
        <ul>
        <li>
            <ul>
                <li>
                    <h2><%= servicesinfo[:service_name] %><h2>
                </li>
                <% servicesinfo.each do |servicedetails| %>
                <li>
                    Host <%= servicedetails[:hostidn] %> - <%= servicedetails[:status] %>
                </li>
                <% end %>
            </ul>

        </li>
        </ul>
    <% end %>
</div>

此哈希的JSON等价物是

{"status":"successful","service_list":[{"service_name":"oozie","status":"RUNNING","status_message":"Running Master Service","host":"1"},{"service_name":"single-namenode","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"single-database","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"datanode","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"secondarynamenode","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"web","status":"DEAD","status_message":"Running Master Service","host":"1"},{"service_name":"tasktracker","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"jobtracker","status":"RUNNING","status_message":"Running Master Service","host":"1"}]}

1 个答案:

答案 0 :(得分:2)

您已经使用服务信息迭代了哈希数组(重命名为有意义):

<% @service_hash[:service_list].each do |service_info| %>
  ...
<% end %>

迭代service_info会返回[key, value]对 - 可能不是你想要的。

直接从service_info访问信息,就像您使用:name

一样
<%= service_info[:host] %> - <%= service_info[:status] %>

我在该哈希中看不到任何名为:hostidn的内容,只有:host;不确定这是否是一个错字,或者您是否期望未显示其他数据。