# Nil Object where you didn't expect it

**URL:** <https://www.ruby-forum.com/t/nil-object-where-you-didnt-expect-it/140817>\
**Category:** Rails\
**Created:** [June 18, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/nil-object-where-you-didnt-expect-it/140817 "2008-06-18T21:55:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [June 18, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/nil-object-where-you-didnt-expect-it/140817/1 "2008-06-18T21:55:10Z")

</div>

So I generate a scaffold with Rails 2.1.0 with a number of database  
fields.

item:string accession\_number:text description:text designer:string  
classification:string overall\_dimensions:string auxiliary\_parts:string  
overall\_condition:string condition\_description:text picture:binary  
accession\_date:datetime provenance:text manufacturer:string  
design\_date:string construction\_date:datetime  
material\_surface\_finish:string medium\_technique:string  
identification\_markings:string picture2:binary recommendations:string  
exhibition\_record:string conservation\_record:string bibliography:text  
dimensions:string notes:text class:string old\_box\_numbers:string  
other\_identification\_numbers:string inventory\_date:datetime  
inventory\_recorded\_by:string other\_source\_location\_information:text  
other\_location\_information:text location:text box\_number:integer  
collector:string collector\_or\_donor:string  
archaeological\_site\_name:string archaeology\_site\_number:string  
geographical\_region:string picture1desc:text picture2desc:text  
insurance\_appraisal:string

And when I go to the scaffold new view, it tells me I have a nill  
object where I didn’t expect it. Normally, this happens when I  
haven’t specified the id of an Active Record object properly so the  
Controller doesn’t give the View a specific active Record object to  
play with.

But in this case the model is simply Entry and my controller code is  
simply @entry=Entry.new. I can’t see where that wouldn’t give it an  
object to play with?

Ron

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [June 18, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/nil-object-where-you-didnt-expect-it/140817/2 "2008-06-18T22:01:39Z")

</div>

On Jun 18, 2008, at 3:54 PM, Ron wrote:

> exhibition\_record:string conservation\_record:string bibliography:text  
> dimensions:string notes:text class:string old\_box\_numbers:string

OK, this is more of a wild guess than anything, but could your column  
‘class’ be a problem? If not, you probably have to look at the link  
in your view where the error is thrown.

> haven’t specified the id of an Active Record object properly so the  
> Controller doesn’t give the View a specific active Record object to  
> play with.
> 
> But in this case the model is simply Entry and my controller code is  
> simply @entry=Entry.new. I can’t see where that wouldn’t give it an  
> object to play with?
> 
> Ron

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [June 20, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/nil-object-where-you-didnt-expect-it/140817/3 "2008-06-20T18:37:19Z")

</div>

Hi Rob,

So I tried changing class to type, without any success. I checked  
the development log and found the following entry:  
ActionView::TemplateError (You have a nil object when you didn’t  
expect it!  
The error occurred while evaluating nil.generated\_methods) on line #8  
of entries/new.html.erb:  
5:  
6:

7: \<%= f.label :dimensions %\>  
  
8: \<%= f.text\_field :dimensions %\>  
9:

  
10: 

11: \<%= f.label :cultural\_affiliation %\>

```
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/

```

attribute\_methods.rb:343:in `respond_to?' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/ helpers/form_helper.rb:622:in `value\_before\_type\_cast’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_view/  
helpers/form\_helper.rb:612:in `value_before_type_cast' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/ helpers/form_helper.rb:535:in `to\_input\_field\_tag’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_view/  
helpers/form\_helper.rb:358:in `text_field' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/ helpers/form_helper.rb:709:in `send’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_view/  
helpers/form\_helper.rb:709:in `text_field' app/views/entries/new.html.erb:8:in `\_run\_erb\_47app47views47entries47new46html46erb’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_view/  
helpers/form\_helper.rb:317:in `fields_for' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/ helpers/form_helper.rb:253:in `form\_for’  
app/views/entries/new.html.erb:3:in  
`_run_erb_47app47views47entries47new46html46erb' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/ base.rb:338:in `send’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_view/  
base.rb:338:in `execute' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/ template_handlers/compilable.rb:29:in `send’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_view/  
template\_handlers/compilable.rb:29:in `render' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/ template.rb:35:in `render’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_view/  
template.rb:22:in `render_template' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/ base.rb:245:in `render\_file’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
base.rb:1108:in `render_for_file' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ base.rb:865:in `render\_with\_no\_layout’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
base.rb:880:in `render_with_no_layout' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ layout.rb:251:in `render\_without\_benchmark’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
benchmarking.rb:51:in `render' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/ core_ext/benchmark.rb:8:in `realtime’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
benchmarking.rb:51:in `render' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ mime_responds.rb:131:in `send’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
mime\_responds.rb:131:in `custom' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ mime_responds.rb:160:in `call’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
mime\_responds.rb:160:in `respond' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ mime_responds.rb:154:in `each’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
mime\_responds.rb:154:in `respond' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ mime_responds.rb:107:in `respond\_to’  
app/controllers/entries\_controller.rb:29:in `new' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ base.rb:1162:in `send’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
base.rb:1162:in `perform_action_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ filters.rb:580:in `call\_filters’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
filters.rb:573:in `perform_action_without_benchmark' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ benchmarking.rb:68:in `perform\_action\_without\_rescue’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/1.8/benchmark.rb:293:in `measure' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ benchmarking.rb:68:in `perform\_action\_without\_rescue’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
rescue.rb:201:in `perform_action_without_caching' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ caching/sql_cache.rb:13:in `perform\_action’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active\_record/  
connection\_adapters/abstract/query\_cache.rb:33:in `cache' /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ query_cache.rb:8:in `cache’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
caching/sql\_cache.rb:12:in `perform_action' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ base.rb:529:in `send’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
base.rb:529:in `process_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ filters.rb:569:in `process\_without\_session\_management\_support’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
session\_management.rb:130:in `process' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ base.rb:389:in `process’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
dispatcher.rb:149:in `handle_request' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ dispatcher.rb:107:in `dispatch’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
dispatcher.rb:104:in `synchronize' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ dispatcher.rb:104:in `dispatch’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/  
dispatcher.rb:120:in `dispatch_cgi' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/ dispatcher.rb:35:in `dispatch’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/rails.rb:76:in `process' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/rails.rb:74:in `synchronize’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/rails.rb:74:in `process' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:159:in `process\_client’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:158:in `each' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:158:in `process\_client’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in `run' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in `initialize’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in `new' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in `run’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in `initialize' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in `new’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in `run' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/configurator.rb:282:in `run’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/configurator.rb:281:in  
`each' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/configurator.rb:281:in `run’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/gems/1.8/gems/mongrel-1.1.4/bin/mongrel\_rails:128:in `run' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/command.rb:212:in `run’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
ruby/gems/1.8/gems/mongrel-1.1.4/bin/mongrel\_rails:281  
/usr/bin/mongrel\_rails:19:in `load’  
/usr/bin/mongrel\_rails:19

Rendering /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/templates/rescues/layout.erb (internal\_server\_error)

Any thoughts?

Thanks,

Ron

On Jun 18, 1:01 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [June 20, 2008, 7:03pm UTC](https://www.ruby-forum.com/t/nil-object-where-you-didnt-expect-it/140817/4 "2008-06-20T19:03:56Z")

</div>

Ron,  
First, ‘type’ isn’t much better than ‘class’ since ‘type’ is used to  
indicate Single-Table Inheritance in ActiveRecord.

Now, I first looked to see if ‘dimensions’ was actually a database  
column. Seems like it is (and spelled consistently in both places,  
too). I can’t try to replicate your error because I don’t have 2.1  
installed, but can you avoid it by commenting out line 8? (\<%# is an  
erb comment)

Do other text\_field calls for this object then work? From the low  
line number, I’m guessing that this is the first text\_field in your  
view.

I may not be helping you much, but this is the kind of controlled  
descent I’d make into the rabbit hole.

-Rob

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [June 20, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/nil-object-where-you-didnt-expect-it/140817/5 "2008-06-20T19:19:50Z")

</div>

So I tried changing type/class to something completely random–in this  
case binder. (I call my variables that when I’m frustrated–I know,  
its weird).

So, its still coming up. I even tried changing dimensions value when  
I did that. I wonder if my name for the controller is reserved,  
entry? None of the other text fields for the scaffold actually work.

Really strange.

btw… I LOVE that phrase “controlled descent into the rabbit hole.”  
I’m going to use it down the road, if that’s ok.

Ron

On Jun 20, 10:03 am, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [June 20, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/nil-object-where-you-didnt-expect-it/140817/6 "2008-06-20T19:50:23Z")

</div>

Ahaha…so I fixed it. It was just that entry, my name for the model,  
was a reserved word.

R

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [June 20, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/nil-object-where-you-didnt-expect-it/140817/7 "2008-06-20T18:50:17Z")

</div>

I have seen the error message

The error occurred while evaluating nil.generated\_methods)

when you use a Rails reserved name

field name “class” is most common offender, may be “type” as well.

Change it to something totally different and see what happens.

-Hari

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [June 20, 2008, 7:54pm UTC](https://www.ruby-forum.com/t/nil-object-where-you-didnt-expect-it/140817/8 "2008-06-20T19:54:31Z")

</div>

Glad you found it. The phrase is free to use, but only if you use it  
wisely. 😉

-Rob

On Jun 20, 2008, at 1:49 PM, Ron wrote:

> > its weird).  
> > Ron
> > 
> > > too). I can’t try to replicate your error because I don’t have 2.1
> > 
> > > > expect it!
> > 
> > > > /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action\_view/  
> > > > `_run_erb_47app47views47entries47new46html46erb' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/ action_controller/ /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/ mime_responds.rb:160:in `call’  
> > > > action\_controller/
> > 
> > > > > > item:string accession\_number:text description:text  
> > > > > > exhibition\_record:string conservation\_record:string  
> > > > > > inventory\_recorded\_by:string  
> > > > > > the  
> > > > > > Ron
> > 
> > > > > Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> > > > > removed\_email\_address@domain.invalid

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid  
+1 513-295-4739  
Skype: rob.biedenharn

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [June 25, 2008, 2:24am UTC](https://www.ruby-forum.com/t/nil-object-where-you-didnt-expect-it/140817/9 "2008-06-25T02:24:02Z")

</div>

Hmmm…  
I have a table called ‘entries’ and a model named ‘entry’ in my app  
and it works.  
Rails 2.0.2
