Haml print at same line - haml

I want to print HAML at same line
%ul
- #categories.each do |c|
%li= c.name
(#{c.posts.count})
and I recive
<ul>
<li>vim</li>
(3)
<li>bash</li>
(1)
</ul>
How to get
<ul>
<li>vim(3)<li>
<li>bash(1)</li>
</ul>

Something like this should work:
%li= "#{c.name} (#{c.posts.count})"

Related

Rails errors.full_messages rendering square brackets

I'm looping through errors in a user#update using the following HAML:
- if #user.errors.any?
.alert.alert-error
%ul
= #user.errors.full_messages.each do |msg|
%li= msg
However, instead of just displaying the message, I also get square brackets:
First name can't be blank ["First name can't be blank"]
What am I doing wrong?
EDIT:
If I do p msg then the console tail shows only the message, but within the <li> the brackets still render.
I figured it out.
The line:
= #user.errors.full_messages.each do |msg|
should have read:
- #user.errors.full_messages.each do |msg|
Haml was printing out the array on top of doing the loop.

Rails group_by and in_groups_of error

I have a list of organisations, grouped and displayed by their name, in alphabetical order. I want to display these across 4 columns for each letter, i.e.:
A
A... A... A... A...
A... A... A... A...
...
Z
Z... Z...
I have used the following code:
<% #organisations.keys.sort.each do |starting_letter| %>
<div class="page-chunk default">
<h6><%= starting_letter %></h6>
<% #organisations[starting_letter].each do |organisations| %>
<% organisations.in_groups_of(4).each do |column| %>
<div class="one_quarter">
<% column.each do |organisation| %>
<%= link_to organisation.name, organisation_path(organisation) %><br />
<% end %>
</div>
<% end %>
<% end %>
</div>
<% end %>
And in the controller:
#organisations = Organisation.all.group_by{ |org| org.name[0] }
But get undefined methodin_groups_of' for #for my troubles. If I change the code to#organisations[starting_letter].in_groups_of(4).each do |organisations|then I get aNilClass` error.
What have I done wrong and how should I fix it?
Try organisations.in_groups_of(4, false)
Without the false, it will fill in any empty spots in the last group with nils, which means it will try to call name on nil.

Grouping records within a view in Rails 3?

I have the following code in my Rails 3 application:
Show view (from another model called animal.rb)
<% #animal.labs.each do |lab| %>
<li>
<%= lab.TestDone.strftime("%d %b. %Y") %>
<h5><%= lab.TestType %></h5>
<h6><%= lab.TestLower %></h6>
<b><%= lab.TestResult %></b>
<h6><%= lab.TestUpper %></h6>
</li>
<% end %>
lab model
belongs_to :animal
default_scope :order => "TestDone DESC", :group => "TestDone"
The above code successfully group all results by date. However, it only shows the first of each value after the date.
For example, if the output was originally (before the grouping) like this:
<li>
26 April 2012
<h5>Glucose</h5>
<h6>1.0</h6>
<b>1.8</b>
<h6>2.0</h6>
</li>
<li>
26 April 2012
<h5>WBC</h5>
<h6>1.1</h6>
<b>2.8</b>
<h6>3.0</h6>
</li>
<li>
26 April 2012
<h5>ABC</h5>
<h6>1.0</h6>
<b>1.6</b>
<h6>2.0</h6>
</li>
then it would output the following after the grouping:
<li>
26 April 2012
<h5>Glucose</h5>
<h6>1.0</h6>
<b>1.8</b>
<h6>2.0</h6>
</li>
What is the correct way of producing something like this:
<li>26 April 2012</li>
<li>
<h5>Glucose</h5>
<h6>1.0</h6>
<b>1.8</b>
<h6>2.0</h6>
</li>
<li>
<h5>WBC</h5>
<h6>1.1</h6>
<b>2.8</b>
<h6>3.0</h6>
</li>
<li>
<h5>ABC</h5>
<h6>1.0</h6>
<b>1.6</b>
<h6>2.0</h6>
</li>
So I have a date heading and then every record with that date is listed below?
A simpler way would be to use group_by like so:
<% #animal.labs.group_by{|l| l.TestDone.strftime("%d %b. %Y") }.each do |testdate,labs| %>
<li><%= testdate %></li>
<% labs.each do |lab| %>
<li>
<h5><%= lab.TestType %></h5>
<h6><%= lab.TestLower %></h6>
<b><%= lab.TestResult %></b>
<h6><%= lab.TestUpper %></h6>
</li>
<% end %>
<% end %>
http://api.rubyonrails.org/classes/Enumerable.html#method-i-group_by
Grouping not works the way you want to use it. First, yo ushould not group it in your default_scope, but you may use some aggreagation in the view, or in the controller.
Model:
default_scope :order => "TestDone DESC"
View:
<% prev = nil %>
<% #animal.labs.each do |lab| %>
<%= "</ul></li>" if prev != nil && prev != lab.TestDone %>
<%= "<li>"+lab.TestDone.strftime("%d %b. %Y")+"<ul>" if prev != lab.TestDone %>
<li>
<h5><%= lab.TestType %></h5>
<h6><%= lab.TestLower %></h6>
<b><%= lab.TestResult %></b>
<h6><%= lab.TestUpper %></h6>
</li>
<% prev = lab.TestDone %>
<% end %>
<%= "</ul></li>" if prev != nil %>
Not the most pretty solution, but it is based on your code.
Basically you need to store the last element's date, in order to compare it with the actual element's date. If they differ, then the new date must be shown. Because they ordered by date, the elements with the same date are close to each other.
I think that is what you was looking for.

Rails 3 displaying count inside a loop

The following displays a list of documents grouped by subject and the name of each document is the name of the packet type. How do I display the the count for each packet type name? so for example if there are two documents for the first subject and their packet type names are 'class' how do I display 'class 1 of 2' and class 2 of 2' next to the packet type name?
controller:
class DevelopController < ApplicationController
def index
list
render('list')
end
def list
#subjects = Subject.includes(:documents => :packet_type)
end
end
view:
<ol>
<% #subjects.each do |subject| %>
<li><%= subject.subject_name %>
<ul>
<% subject.documents.each do |document| %>
<li><%= document.packet_type.name %></li>
</li>
<% end %>
</ul>
<% end %>
</ol>
You can get the total count with .size or .count. To get the current index, you can use each_with_index instead of each. And here on a silver plate : http://apidock.com/ruby/Enumerable/each_with_index :)

Altering nested messages helper from div to unordered list

I am using a helper method from ryan bates railscasts on ancestry to display nested messages(code below works perfectly).
def nested_messages(messages)
messages.map do |message, sub_messages|
render(message) + content_tag(:div, nested_messages(sub_messages), :class => "nested_messages")
end.join.html_safe
end
The above bit of code nests the individual divs in a tree like structure. I would like to make this into an unordered list, so what i have done is this:
def nested_messages(messages)
messages.map do |message, sub_messages|
content_tag(:ul, :class => "") do
render(message)
content_tag(:li, :class => "nested_messages") do
nested_messages(sub_messages)
end
end
end.join.html_safe
end
The generated html looks fine, however the list items contain no values. Am i doing something wrong?
UPDATE
I would like the generated html to look like this:
<ul>
<li>Main Message</li> <!-- first message -->
<li>
<b>Message 1</b>
<ul>
<li>Message 1 subchild 1</li>
<li>Message 1 subchild 2</li>
</ul>
</li>
</ul>
UPDATE 2
I have changed it to this and it works, thanks to Dave:
def nested_messages(messages)
messages.map do |message, sub_messages|
#render(message) + content_tag(:div, sub_messages, :class => "nested_messages")
content_tag(:ul, :class => "") do
content_tag(:li, :class => "nested_messages") do
render(message) + nested_messages(sub_messages)
end
end
end.join.html_safe
end
You create a ul tag, then render the message. If you do that, what will your HTML look like?
Things inside a ul should be in a nested li: you just render the message.
You need to put it in an li tag so the unordered list has valid content.