Please help. The error occurred while evaluating nil.name

Okay so heres my code which works fine, i'll explain the problem below.

<% for num in 1..@results.length %> <table class="products" id="table1" cellspacing="2" cellpadding="3" border="3">   <tr>     <td>   <table width="25%">     <tr>       <tr>         <td><%= @results[1].name%> | <%= number_to_currency(@results[1].price)%></td>       </tr>       <tr>         <td><%= link_to image_tag(@results[1].image_url, :size => '100x100', :border => "0")%></td>       </tr>       <tr>         <td><%= h(truncate(@results[1].description, 150))%></td>       </tr>     </tr>   </table>   <td> </table> <%end%>

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.

Okay so heres my code which works fine, i'll explain the problem below.

<% for num in 1..@results.length %>

array indices from from 0 to length -1, not from 1 to length

Fred

> Okay so heres my code which works fine, i'll explain the problem > below.

> <% for num in 1...@results.length %>

array indices from from 0 to length -1, not from 1 to length

Fred

will you marry me

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.each do |product| %> ... <td><%= product.name%> | <%= number_to_currency(product.price)%></td>

This version expresses your intentions much more clearly, which will make it easier to maintain in the future..

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.

<div id="products"> <table cellspacing="2" cellpadding="3" border="3">   <% for prods in 0..@stop %>   <tr>     <%for i in 0..3%>     <td>   <table width="25%">       <tr>         <td><%= @results[i].name%>| <%= number_to_currency(@results[i].price)%></td>       </tr>       <tr>         <td><%= link_to image_tag(@results[i].image_url, :size => '100x100', :border => "0")%></td>       </tr>       <tr>         <td><%= h(truncate(@results[i].description, 150))%></td>       </tr>   </table>   </td>     <%end%>   </tr>   <%end%> </table> <% for whatsleft in 0..0%> <%newstop = (@results.length) - 1%> <table class="products" id="table1" cellspacing="2" cellpadding="3" border="3">   <tr>   <%for i in 0..@whatsLeft - 1%>   <td>     <table width="25%">         <tr>           <td><%= @results[newstop].name%>| <%= number_to_currency(@results[newstop].price)%></td>         </tr>         <tr>           <td><%= link_to image_tag(@results[newstop].image_url, :size => '100x100', :border => "0")%></td>         </tr>         <tr>           <td><%= h(truncate(@results[newstop].description, 150))%></td>         </tr>     </table>     <%newstop -= 1%>     <%end%>   </td>   <%end%>   </tr> </table> </div>

nameth wrote:

<% for num in 1...

Next, never use 'for .. in' in Ruby. Matz added it just to look like lessor languages; to occupy the third page of a Ruby tutorial.

Use names.each do |name| instead.

And BTW everyone knows that online romances never last...

nameth,

You don't have to resort to old-school indexed iteration to solve this type of problem. Have a look at Rails' 'grouping' extentions to Array:

http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Grouping.html

For example:

<table>   <% @results.in_groups_of(4).compact do |result_group| %>     <tr>       <% result_group.each do |product| %>         <td>           <%= h product.name %>| <%= number_to_currency product.price %>         </td>       <% end %>     </tr>     <tr>       <% result_group.each do |product| %>         <td>           <%= link_to image_tag(product.image_url, :size => '100x100', :border => "0") %>         </td>       <% end %>     </tr>     <tr>       <% result_group.each do |product| %>         <td>           <%= h(truncate(product.description, 150)) %>         </td>       <% end %>     </tr>   <% end %> </table>

Should produce similar output, but with DRYer, cleaner code.

- Barney

Thanks Barney, Exactly what i was looking for.