# Beginner Error: You have a nil object

**URL:** <https://www.ruby-forum.com/t/beginner-error-you-have-a-nil-object/91519>\
**Category:** Rails\
**Created:** [April 10, 2007, 2:09am UTC](https://www.ruby-forum.com/t/beginner-error-you-have-a-nil-object/91519 "2007-04-10T02:09:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 10, 2007, 2:09am UTC](https://www.ruby-forum.com/t/beginner-error-you-have-a-nil-object/91519/1 "2007-04-10T02:09:11Z")

</div>

So I set out to build an app that is very similar to the Depot app to  
build my experience. I added a lot of constant lists for the  
pulldowns on the admin add product page. Then this error?

NoMethodError in Admin#new

Showing app/views/admin/\_form.rhtml where line #9 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.select

Extracted source (around line #9):

6:  
7:

Newused  
  
8: \<%=  
9: form.select :newused,  
10: Vehicle::NEWUSED,  
11: :prompt =\> “New or used vehicle?”  
12: %\>

from vehicle.rb

NEWUSED = [  
[“New”, “New”],  
[“Used”, “Used”]  
]

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 10, 2007, 2:16am UTC](https://www.ruby-forum.com/t/beginner-error-you-have-a-nil-object/91519/2 "2007-04-10T02:16:11Z")

</div>

On 4/9/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> You might have expected an instance of Array.  
> 12: %\>

The error is related to the ‘form’ variable being nil, not your  
NEWUSED constant. So start looking there. Where is form defined?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 10, 2007, 2:26am UTC](https://www.ruby-forum.com/t/beginner-error-you-have-a-nil-object/91519/3 "2007-04-10T02:26:13Z")

</div>

I don’t, I thought it was universal: form.select and form.text\_field  
and form.text\_area. Hrm. I’m assuming that I missed one of these  
guys:

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 10, 2007, 2:39am UTC](https://www.ruby-forum.com/t/beginner-error-you-have-a-nil-object/91519/4 "2007-04-10T02:39:14Z")

</div>

On 4/9/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > Showing app/views/admin/\_form.rhtml where line #9 raised:  
> > > 9: form.select :newused,

You just added a div field, which certainly doesn’t do anything as far  
as Ruby is concerned.

You’re probably missing something like

\<% form\_for :something, :url =\> some\_url, do |form| %\>

Take a closer look at whatever code you’re copying. It’ll show you  
exactly what you need to do.

Pat

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 10, 2007, 3:31am UTC](https://www.ruby-forum.com/t/beginner-error-you-have-a-nil-object/91519/5 "2007-04-10T03:31:17Z")

</div>

Ok, I’m getting closer. This gives me the pull-down with NewNew in it  
– I need it to be New or Used and not printed twice.

Newused  
\<%= select 'vehicle\_newused','newused', [Vehicle::NEWUSED] %\>

On Apr 9, 9:14 pm, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 10, 2007, 6:56am UTC](https://www.ruby-forum.com/t/beginner-error-you-have-a-nil-object/91519/6 "2007-04-10T06:56:32Z")

</div>

This appears to be the answer, thanks to AgileROR:

Newused  
\<%= select (:vehicle, :newused, Vehicle::NEWUSED, :prompt =\> "New or used?") %\>

On Apr 9, 9:30 pm, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 10, 2007, 3:16am UTC](https://www.ruby-forum.com/t/beginner-error-you-have-a-nil-object/91519/7 "2007-04-10T03:16:16Z")

</div>

Ok, you are correct. I found out the problem but that led to another  
problem. I’m converted a dynamic scaffold to a static and then edited  
the rhtml so that all of the fields wouldn’t be textboxes. In this  
case I want a select pulldown and have two options to choose from. I  
just removed the form. on each one and now I’ve got a page with the  
pulldowns with one option: prompt.
