Reputation: 157
I currently have a table that is split into 4 columns as such:
<table id="customers">
<tr>
<th><em class='require'></em><%= Release.columns_hash['version'].human_name -%></th>
<th><em class='require'></em><%= Release.columns_hash['version'].human_name -%></th>
<th><em class='require'></em><%= Release.columns_hash['version'].human_name -%></th>
<th><em class='require'></em><%= Release.columns_hash['version'].human_name -%></th>
</tr>
<% @releases.in_groups_of(4).each do |releasesplit| %>
<tr>
<% releasesplit.each do |release| %>
<td align="center"><%= release.version %></td>
<% end %>
</tr>
<% end %>
However the problem is whenever I add another entry to the table that is not divisible by 4 I get the error: undefined method version' for nil:NilClass
Any Ideas how to fix this? Thanks,
Upvotes: 0
Views: 367
Reputation: 21100
It seems like one of your releases in @releases
variable is nil
.
You can do this:
@releases.compact.in_groups_of(4).each do ...
Array#compact
returns a copy of the original array with nil
elements removed.
However, I strongly suggest you investigate why one of those is nil in the first place.
This is not the issue of grouping, since this is how grouping works:
irb(main):002:0> [1,2,3].each_slice(2) {|group| puts group.inspect}
[1, 2]
[3]
=> nil
(in_groups_of
is just an ActiveSupport alias for each_slice
)
Upvotes: 4