# Please help. The error occurred while evaluating nil.name

**URL:** <https://discuss.rubyonrails.org/t/please-help-the-error-occurred-while-evaluating-nil-name/28518>\
**Category:** rubyonrails-talk\
**Created:** [April 24, 2008, 2:51pm UTC](https://discuss.rubyonrails.org/t/please-help-the-error-occurred-while-evaluating-nil-name/28518 "2008-04-24T14:51:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JMcG1](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@JMcG1](https://discuss.rubyonrails.org/u/JMcG1)\
**Post date:** [April 24, 2008, 2:51pm UTC](https://discuss.rubyonrails.org/t/please-help-the-error-occurred-while-evaluating-nil-name/28518/1 "2008-04-24T14:51:52Z")

</div>

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"\> &nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\> &nbsp;&nbsp;\<table width="25%"\> &nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= @results[1].name%\> | \<%= number\_to\_currency(@results[1].price)%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= link\_to image\_tag(@results[1].image\_url, :size =\> '100x100', :border =\> "0")%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= h(truncate(@results[1].description, 150))%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;\</table\> &nbsp;&nbsp;\<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.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 24, 2008, 3:04pm UTC](https://discuss.rubyonrails.org/t/please-help-the-error-occurred-while-evaluating-nil-name/28518/2 "2008-04-24T15:04:03Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![JMcG1](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@JMcG1](https://discuss.rubyonrails.org/u/JMcG1)\
**Post date:** [April 24, 2008, 3:16pm UTC](https://discuss.rubyonrails.org/t/please-help-the-error-occurred-while-evaluating-nil-name/28518/3 "2008-04-24T15:16:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AndyV](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AndyV](https://discuss.rubyonrails.org/u/AndyV)\
**Post date:** [April 24, 2008, 4:43pm UTC](https://discuss.rubyonrails.org/t/please-help-the-error-occurred-while-evaluating-nil-name/28518/4 "2008-04-24T16:43:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JMcG1](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@JMcG1](https://discuss.rubyonrails.org/u/JMcG1)\
**Post date:** [April 24, 2008, 4:54pm UTC](https://discuss.rubyonrails.org/t/please-help-the-error-occurred-while-evaluating-nil-name/28518/5 "2008-04-24T16:54:09Z")

</div>

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"\> &nbsp;&nbsp;\<% for prods in 0..@stop %\> &nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<%for i in 0..3%\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\> &nbsp;&nbsp;\<table width="25%"\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= @results[i].name%\>| \<%= number\_to\_currency(@results[i].price)%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= link\_to image\_tag(@results[i].image\_url, :size =\> '100x100', :border =\> "0")%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= h(truncate(@results[i].description, 150))%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;\</table\> &nbsp;&nbsp;\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<%end%\> &nbsp;&nbsp;\</tr\> &nbsp;&nbsp;\<%end%\> \</table\> \<% for whatsleft in 0..0%\> \<%newstop = (@results.length) - 1%\> \<table class="products" id="table1" cellspacing="2" cellpadding="3" border="3"\> &nbsp;&nbsp;\<tr\> &nbsp;&nbsp;\<%for i in 0..@whatsLeft - 1%\> &nbsp;&nbsp;\<td\> &nbsp;&nbsp;&nbsp;&nbsp;\<table width="25%"\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= @results[newstop].name%\>| \<%= number\_to\_currency(@results[newstop].price)%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= link\_to image\_tag(@results[newstop].image\_url, :size =\> '100x100', :border =\> "0")%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= h(truncate(@results[newstop].description, 150))%\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;\</table\> &nbsp;&nbsp;&nbsp;&nbsp;\<%newstop -= 1%\> &nbsp;&nbsp;&nbsp;&nbsp;\<%end%\> &nbsp;&nbsp;\</td\> &nbsp;&nbsp;\<%end%\> &nbsp;&nbsp;\</tr\> \</table\> \</div\>

---

<div class="post-metadata">

**Author:** ![Phlip](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Phlip](https://discuss.rubyonrails.org/u/Phlip)\
**Post date:** [April 25, 2008, 1:52am UTC](https://discuss.rubyonrails.org/t/please-help-the-error-occurred-while-evaluating-nil-name/28518/6 "2008-04-25T01:52:25Z")

</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...

---

<div class="post-metadata">

**Author:** ![Barney](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barney](https://discuss.rubyonrails.org/u/Barney)\
**Post date:** [April 26, 2008, 11:04am UTC](https://discuss.rubyonrails.org/t/please-help-the-error-occurred-while-evaluating-nil-name/28518/7 "2008-04-26T11:04:55Z")

</div>

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](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Grouping.html)

For example:

\<table\> &nbsp;&nbsp;\<% @results.in\_groups\_of(4).compact do |result\_group| %\> &nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% result\_group.each do |product| %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= h product.name %\>| \<%= number\_to\_currency product.price %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% result\_group.each do |product| %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= link\_to image\_tag(product.image\_url, :size =\> '100x100', :border =\> "0") %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% result\_group.each do |product| %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= h(truncate(product.description, 150)) %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;\<% end %\> \</table\>

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

- Barney

---

<div class="post-metadata">

**Author:** ![JMcG1](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@JMcG1](https://discuss.rubyonrails.org/u/JMcG1)\
**Post date:** [May 1, 2008, 4:50pm UTC](https://discuss.rubyonrails.org/t/please-help-the-error-occurred-while-evaluating-nil-name/28518/8 "2008-05-01T16:50:30Z")

</div>

Thanks Barney, Exactly what i was looking for.
