jruby issue iterating over multi dimensional hash object - ruby-on-rails

So being new to rails I seem to be stuck on creating a loop within a loop to process the information.
I am getting:
can't convert Symbol into Integer line #11
The line in question is:
Host <%= servicedetails[:hostidn] %> - <%= servicedetails[:status] %>
And here is the full version below. Being new Im clueless and open to suggestions.
<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>
the JSON equivalent of this hash is
{"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"}]}

You're already iterating over the array of hashes with service info (renamed to make sense):
<% #service_hash[:service_list].each do |service_info| %>
...
<% end %>
Iterating over service_info would return [key, value] pairs--likely not what you want.
Access the information from service_info directly, as you already do with :name
<%= service_info[:host] %> - <%= service_info[:status] %>
I don't see anything called :hostidn in that hash, just :host; not sure if that's a typo, or if you're expecting additional data not shown.

Related

Strange output from rails each do

Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>

Rails render partial with block and collection

This is similar to this question but with a collection:
<div class="panel-body">
<%= render layout: 'today_items_list', locals: {items: #pos} do |po| %>
<% #output_buffer = ActionView::OutputBuffer.new %>
<%= link_to "##{po.id}", po %>
<%= po.supplier.name %>
<% end %>
</div>
with partial/layout:
.tableless_cell.no_padding
%h3.no_margin_vertical= title
%ul.no_margin_vertical
- for item in items
%li= yield(item)
This renders as you expect but if I omit the weird '#output_buffer = ActionView::OutputBuffer.new', the buffer is not cleared and the list is rendered this way:
<li>
#4833Supplier name
</li>
<li>
#4833Supplier name
#4835Supplier name 2
</li>
<li>
#4833Supplier name
#4835Supplier name 2
#4840Supplier name 3
</li>
Never clearing the buffer between block invocation. What am I missing here?
(Riding on Rails 3.2.22)
I don't have much experience with Rails 3.2; with newer versions, you're able to pass collections to partials:
<%= render #pos, layout: 'today_items_list' %>
#app/views/pos/_today_items_list.html.erb
.tableless_cell.no_padding
%h3.no_margin_vertical= title
%ul.no_margin_vertical
<%= yield %>
#app/views/post/_po.html.erb
<%= link_to "##{po.id}", po %>
<%= po.supplier.name %>
You can read more about collections etc (I presume for Rails 4+) here.

Ruby on Rails how to get last query?

I'm having trouble trying to figure out when I reached the end of my query. So what I want to do is list all the records in my database that begin with the letter A which I got however I want to output a message if the query turns out blank. When I try I get a bunch of my custom messages even the query didn't turn out blank. Is there any way to tell if I've reached EOF in ruby on rails?
Sample
<div id = "content-A">
<p>A</p>
<% #animes.each do |anime| %>
<% if anime.aname.starts_with?('A') %>
<%= link_to anime.aname, {:action => 'list'} %>
<% else %>
<p>No anime listed in this Category :( </p>
<%end%>
<%end %>
</div>
I believe you want sth like:
<% animes_group = #animes.group_by {|anime| anime.aname.to_s[0].upcase}
('A'..'Z').each do |letter| %>
<div id="content-<%= letter %>">
<p><%= letter %></p>
<% if animes = animes_group[letter] %>
<% animes.each do |anime| %>
<%= link_to anime.aname, {:action => 'list'} %>
<% end %>
<% else %>
<p>No anime listed in this Category :( </p>
<%end%>
<% end %>
You should consider moving some of the logic to the controller here, however what is to be moved depends on many factors like whether #animes are being used anywhere else etc.

Can one use conditions and loops on a single line in Ruby?

How would one go about turning the following code into the latter?
<div id="faqs">
<% if #faqs.length > 0 %>
<% #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end %>
<% else %>
<p>No FAQs to display.</p>
<% end %>
</div>
<div id="faqs">
<% #faqs.empty? ? content_tag(:p, "No FAQs to display.") : #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end %>
</div>
I'm curious as to whether I can get the latter code to work. The only element of it that is failing at the moment is that the content_tag() is not displaying - this is due to the fact that I'm not using printable ruby tags (<%= # %>) but using them will dump out the FAQ objects underneath the content.
I considered the use of puts() to print the content_tag() while inside the ruby tags but that didn't work.
I've tried to search for this issue but haven't yielded anything useful.
Is this achievable and if so, does it have any benefits other than being prettier?
One way to make the later code to work if you can put the body of the loop in a helper function and return the out put of content_tag from that. The line in view file might be somewhat like this.
<%= #faqs.empty? ? content_tag(:p, "No FAQs to display.") : printList(#faqs) %>
and your printList function will return the output of nested content_tags. You can make a generic list printing function which can be used for any list.
Something so obvious but still shared.
This should work (for clarity, I moved FAQ tag generation in separate helper method):
<div id="faqs">
<%= raw (#faqs.empty? ? content_tag(:p, "No FAQs to display.") : #faqs.map { |faq| faq_div(faq) }.join) %>
</div>
or, perhaps more clean:
<div id="faqs">
<%= content_tag(:p, "No FAQs to display.") if #faqs.empty? %>
<%= raw #faqs.map { |faq| faq_div(faq) }.join %>
</div>
meanwhile, in helpers:
def faq_div(faq)
'<div class="faq"><strong>Q:</strong> %s<br /><strong>A:</strong> %s</div>' % [faq.question, faq.answer]
end
This should work:
<% if #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end.empty? %>
<p>No FAQs to display.</p>
<% end %>

Why Rails images display #<Photo:0xB4F28FA># object id?

Here is my view:
<div>
<ul>
<%= #album.photos.each do |photo| %>
<li><%= link_to(image_tag(photo.soure.url(:small)),photo.source.url(:medium)) %></li>
<% end %>
</ul>
</div>
produces the right result except all the object ids (i.e. #<Photo:0xXXXXXX>#) get added right before the </ul> and display in the html. I'm guessing since each time the block gets executed it returns the Photo object and that's why its rendering all the #<Photo:0x>s but i don't know how to STOP this from happening.
It's because you have:
<%= #album.photos.each do |photo| %>
instead of:
<% #album.photos.each do |photo| %>

Resources