Rails 迭代视图哈希时出现问题
我有这个哈希。我正在尝试在视图中渲染。我对 Rails 还很陌生,我对如何做到这一点一无所知。我需要做的就是能够按 service_name 或主机对事物进行排序,并据此构建块。我已经尝试了十几种左右的方法,但我似乎无法正确处理此问题,因此我需要此时可以获得的任何帮助。
我想做的一个例子:
下面是哈希值。到目前为止,我得到的最好的只是独立列出的服务名称,无需排序或其他任何内容。
services_hash = {
: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"
},
{
: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"
},
{
: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"
},
{
: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"
},
{
: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"
},
{
: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"
},
{
: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"
}
]
}
编辑 以下是我尝试过的两件事,另一件事是其他人帮助我得出的排序结论。两者都在一定程度上起作用。但当我把这些想法结合起来时,我惨遭失败。从那以后,我放弃了来这里的组合努力,希望找到如何做到这一点的答案,这样我就可以迭代哈希以重现上面的图像以达到某种效果或另一种效果。
请注意,目前我意识到 HTML 的输出不会完全类似于图像。现在我被它的 if-else 部分困住了,因为我尝试的方式似乎跳过了它。
<%
val = @service_hash
svcs = val[:service_list]
puts "#{svcs.size} service entries."
grouped = svcs.group_by { |svc| svc[:service_name] }
puts "#{grouped.size} named services:"
grouped.keys.sort.each do |name|
puts " service: #{name}"
grouped[name].each do |svc|
puts " host: #{svc[:host]}"
puts " status: #{svc[:status]}"
puts " #{svc[:status_message]}"
end
puts '-'*40
end
%>
I have this hash. I am trying to render in a view. Which me being so new to Rails I haven't the slightest clue how to do it. What I need to do over all is be able to sort things by either service_name or by host and build blocks according to that. I've tried a dozen or so things but I can't seem to work with this properly so I need any help I can get at this point.
An example of what I am trying to do:
Below is the hash. Best I've gotten so far is just the Service name to list independently without sorting or anything else.
services_hash = {
: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"
},
{
: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"
},
{
: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"
},
{
: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"
},
{
: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"
},
{
: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"
},
{
: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"
}
]
}
edit
Below is 2 things one I tried, one someone else helped me come to the conclusion of for the sorting. Both work to a point. But when I went to combine the ideas I failed miserably. I have since scrapped my combination effort coming here to hope to find an answer how to do it so I can iterate over the hash to reproduce the above image to some effect or another.
Note currently I realize the output of the HTML would not completely resemble the image. Right now I am am stuck with the if-else portion of it, as the way I am trying it seems to skip over it.
<%
val = @service_hash
svcs = val[:service_list]
puts "#{svcs.size} service entries."
grouped = svcs.group_by { |svc| svc[:service_name] }
puts "#{grouped.size} named services:"
grouped.keys.sort.each do |name|
puts " service: #{name}"
grouped[name].each do |svc|
puts " host: #{svc[:host]}"
puts " status: #{svc[:status]}"
puts " #{svc[:status_message]}"
end
puts '-'*40
end
%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Ruby on Rails 中,
puts
不会写入输出缓冲区。在 ERB 中,<%= %>
内的代码块的返回值将被写入输出。所以在你看来你的代码应该看起来更像这样:In Ruby on Rails
puts
doesn't write to the output buffer. In an ERB the return value of a code block inside<%= %>
will be written to the output. So in your view your code should look more like this: