Rails group_by和in_groups_of错误

Ash*_*Bye 1 group-by ruby-on-rails-3

我有一个组织列表,按名称分组和显示,按字母顺序排列.我想在每个字母的4列中显示这些,即:

A
A... A... A... A...
A... A... A... A...

...

Z
Z... Z... 
Run Code Online (Sandbox Code Playgroud)

我使用了以下代码:

<% @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 %>
Run Code Online (Sandbox Code Playgroud)

在控制器中:

@organisations = Organisation.all.group_by{ |org| org.name[0] }
Run Code Online (Sandbox Code Playgroud)

但是要获取undefined methodin_groups_of'for#for my troubles. If I change the code to@organisations [starting_letter] .in_groups_of(4).each do | organizations | then I get aNilClass`错误.

我做错了什么,应该如何解决?

joh*_*kes 6

尝试organisations.in_groups_of(4, false) 如果没有false,它会在任何空点填充在最后一组nilS,这意味着它将尝试调用namenil.