Please help. The error occurred while evaluating

Okay so heres my code which works fine, i’ll explain the problem

<% for num in [email protected] %>

<%= @results[1].name%> | <%= number_to_currency(@results[1].price)%>
<%= link_to image_tag(@results[1].image_url, :size => '100x100', :border => "0")%>
<%= h(truncate(@results[1].description, 150))%>

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”

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.

On 24 Apr 2008, at 15:51, nameth wrote:

Okay so heres my code which works fine, i’ll explain the problem

<% for num in [email protected] %>
array indices from from 0 to length -1, not from 1 to length


On Apr 24, 10:04 am, Frederick C. [email protected]


will you marry me

Yeah i know i could just use <%for each prod in @results%> and then
reference by <> <%=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.

<% for prods in 0..@stop %> <%for i in 0..3%> <%end%> <%end%>
<%= @results[i].name%>| <%= number_to_currency(@results[i].price)%>
<%= link_to image_tag(@results[i].image_url, :size => '100x100', :border => "0")%>
<%= h(truncate(@results[i].description, 150))%>
<% for whatsleft in 0..0%> <%newstop = (@results.length) - 1%> <%for i in 0..@whatsLeft - 1%> <%end%>
<%= @results[newstop].name%>| <%= number_to_currency(@results[newstop].price)%>
<%= link_to image_tag(@results[newstop].image_url, :size => '100x100', :border => "0")%>
<%= h(truncate(@results[newstop].description, 150))%>
<%newstop -= 1%> <%end%>

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| %>

<%=> | <%= number_to_currency(product.price)%>

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

nameth wrote:

<% for num in 1…

Next, never use ‘for … in’ in Ruby. Matz added it just to look like
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…


Thanks Barney, Exactly what i was looking for.


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:

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 %>| <%= number_to_currency product.price %>
<%= link_to image_tag(product.image_url, :size => '100x100', :border => "0") %>
<%= h(truncate(product.description, 150)) %>

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

  • Barney