# The error occurred while evaluating nil.each

**URL:** <https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil-each/140506>\
**Category:** Rails\
**Created:** [June 15, 2008, 11:41am UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil-each/140506 "2008-06-15T11:41:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoR\_Nb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@RoR\_Nb](https://www.ruby-forum.com/u/RoR_Nb)\
**Post date:** [June 15, 2008, 11:41am UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil-each/140506/1 "2008-06-15T11:41:08Z")

</div>

I’m leaving some fields empty and after i sibmited it shows me teh  
following error,

# THIS IS THE ERROR THAT IT DISPLAYS

NoMethodError in Item\_packages#create

Showing item\_packages/\_form.erb where line #3 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.each

Extracted source (around line #3):

1:   
2: function select\_html\_code(the\_id){  
3: \<% for measurement\_formats in @measurement\_formats %\>  
4: if(the\_id==’\<%= measurement\_formats.id %\>’){  
5: document.getElementById(’\<%= measurement\_formats.id  
%\>’).style.position=‘static’  
6: document.getElementById(’\<%= measurement\_formats.id  
%\>’).style.visibility=‘visible’

# THIS IS MY CONTROLLER:

def new  
@item\_package = ItemPackage.new(:package\_color\_id =\>  
params[:package\_color\_id])  
@measurement\_formats = MeasurementFormat.find(:all)

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @item_package }
end

```

end

the weird thing is that if i fill out the entire form,  
it’ll show no problems?  
any idea to this weird problem?

thanks in advanced

---

<div class="post-metadata">

**Author:** ![RoR\_Nb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@RoR\_Nb](https://www.ruby-forum.com/u/RoR_Nb)\
**Post date:** [June 15, 2008, 11:48am UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil-each/140506/2 "2008-06-15T11:48:10Z")

</div>

On Jun 15, 4:49 am, RoR\_Nb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the weird thing is that if i fill out the entire form,  
> it’ll show no problems?  
> any idea to this weird problem?

If the problem is the create action, it would make sense to show us  
the code for the create action, not the new action. My guess is that  
in your create action you are following the standard pattern of  
rerendering the new template if validation fails. However, in your  
create action you don’t initialize @measurement\_formats, hence the  
error.

Fred

---

<div class="post-metadata">

**Author:** ![RoR\_Nb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@RoR\_Nb](https://www.ruby-forum.com/u/RoR_Nb)\
**Post date:** [June 15, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/the-error-occurred-while-evaluating-nil-each/140506/3 "2008-06-15T14:53:39Z")

</div>

On Sun, Jun 15, 2008 at 5:47 AM, Frederick C. \<  
removed\_email\_address@domain.invalid\> wrote:

> in your create action you are following the standard pattern of  
> rerendering the new template if validation fails. However, in your  
> create action you don’t initialize @measurement\_formats, hence the  
> error.

And, since the nil.each might be a mystery to a nuby, let me point out  
that:

for measurement\_formats in @measurement\_formats  
#…  
end

is just Ruby syntactic sugar for

@measurement\_formats.each do | measurement\_formats |  
#…  
end

## BTW I’d suggest renaming the local measurement\_formats (without the @) to measurement\_format in either case, since it’s going to be a single measurement\_format at any given time.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
