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
%>
In Ruby on Rails
putsdoesn’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: