This works fine, however when i replace the 1’s with num, which
should work. i get the error.
“You have a nil object when you didn’t expect it! The error occurred
while evaluating nil.name.”
I know that num is outputting a number because if i throw <%=num%>
somewhere in the loop it will output all the numbers. I’m stuck on
this i’ve tried everything i can think of, I know there are other ways
to loop through this but i need this way to work. Any help would be
appreciated. Thanks.
Yeah i know i could just use <%for each prod in @results%> and then
reference by <%=prod.name%> <%=prod.price%> etc… , but the thing is
i don’t want to “do” what i’m doing for “each” thing in the @results
collection. here is my finished page, you can probably see why i
couldn’t do it that way.
There’s a better way to use the @results collection, too. Some people
prefer the vb/c# like iteration:
for result in @results do
Personally I don’t like that because it’s one more indirection than
necessary. I prefer the fundamental each operator:
@results.each do |result|
In either case, you can use all the code you’ve got, but replace all
the @results[num] references with result (or something more domain
specific like product…). For example,
<% @results.in_groups_of(4).compact do |result_group| %>
<% result_group.each do |product| %>
<% end %>
<% result_group.each do |product| %>
<% end %>
<% result_group.each do |product| %>
<% end %>
<% end %>
<%= h product.name %>| <%= number_to_currency product.price
%>