# Called id for nil, which would mistakenly be 4 -- if you rea

**URL:** <https://www.ruby-forum.com/t/called-id-for-nil-which-would-mistakenly-be-4-if-you-rea/58417>\
**Category:** Rails\
**Created:** [April 21, 2006, 2:04pm UTC](https://www.ruby-forum.com/t/called-id-for-nil-which-would-mistakenly-be-4-if-you-rea/58417 "2006-04-21T14:04:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shreekar\_Joshi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Shreekar\_Joshi](https://www.ruby-forum.com/u/Shreekar_Joshi)\
**Post date:** [April 21, 2006, 2:04pm UTC](https://www.ruby-forum.com/t/called-id-for-nil-which-would-mistakenly-be-4-if-you-rea/58417/1 "2006-04-21T14:04:19Z")

</div>

Hi

```
I am a newbie to Ruby on Rails, but experienced in programming.

```

I am trying out Rolling with Ruby on Rails article by Curt H. on  
[onlamp.com](http://onlamp.com). I have encountered following error:

Called id for nil, which would mistakenly be 4 – if you really wanted  
the id of nil, use object\_id

```
I searched the archives of the list and found that similar

```

question was left unanswered. I do understand from the error that the  
recipe.id is null(or nil).  
I have come to this page from edit link from the show page. This  
show page is generated by the scaffolding. The url for this link _seems_  
to be correct - recipe/edit/2 (where 2 is id of the recipe).  
Any ideas?

The entire dump is reproduced below:

RuntimeError in Recipe#edit  
Showing app/views/recipe/edit.rhtml where line #10 raised:

Called id for nil, which would mistakenly be 4 – if you really wanted  
the id of nil, use object\_id

Extracted source (around line #10):

7:  
8: \<form action=“…/update/” \<%= @recipe.id %\> method=“POST”\>  
9: \<input id=“recipe\_id” name=“recipe[id]” size=“30”  
10: type=“hidden” value=“\<%= @recipe.id %\>” /\>  
11:

**Title**  
  
12: \<input id=“recipe\_title” name=“recipe[title]” size=“30”  
13: type=“text” value=“\<%= @recipe.title %\>” /\>

RAILS\_ROOT: ./script/…/config/…

Application Trace | Framework Trace | Full Trace  
#{RAILS\_ROOT}/app/views/recipe/edit.rhtml:10:in `_run_rhtml_recipe_edit' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb :314:in `send’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/base.rb  
:314:in `compile_and_render_template' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb :290:in `render\_template’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/base.rb  
:249:in `render_file' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/b ase.rb:699:in `render\_file’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
ase.rb:621:in `render_with_no_layout' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/l ayout.rb:251:in `render\_without\_benchmark’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
enchmarking.rb:53:in `render' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/b enchmarking.rb:53:in `measure’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
enchmarking.rb:53:in `render' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/b ase.rb:911:in `perform\_action\_without\_filters’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/f  
ilters.rb:368:in `perform_action_without_benchmark' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/b enchmarking.rb:69:in `perform\_action\_without\_rescue’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
enchmarking.rb:69:in `measure' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/b enchmarking.rb:69:in `perform\_action\_without\_rescue’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/r  
escue.rb:82:in `perform_action' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/b ase.rb:381:in `send’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
ase.rb:381:in `process_without_filters' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/f ilters.rb:377:in `process\_without\_session\_management\_support’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/s  
ession\_management.rb:117:in `process' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick\_server.rb:115:in  
`handle_dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:81:in `service’  
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick\_server.rb:67:in  
`dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/webrick. rb:59 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require\_\_’  
c:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:18:in  
`require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/de pendencies.rb:147:in `require’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30  
c:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:18:in  
`require__' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require’  
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/de  
pendencies.rb:147:in `require' script/server:3 #{RAILS_ROOT}/app/views/recipe/edit.rhtml:10:in `_run\_rhtml\_recipe\_edit’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/base.rb  
:314:in `send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb :314:in `compile\_and\_render\_template’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/base.rb  
:290:in `render_template' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb :249:in `render\_file’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
ase.rb:699:in `render_file' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/b ase.rb:621:in `render\_with\_no\_layout’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/l  
ayout.rb:251:in `render_without_benchmark' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/b enchmarking.rb:53:in `render’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
enchmarking.rb:53:in `measure' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/b enchmarking.rb:53:in `render’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
ase.rb:911:in `perform_action_without_filters' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/f ilters.rb:368:in `perform\_action\_without\_benchmark’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
enchmarking.rb:69:in `perform_action_without_rescue' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/b enchmarking.rb:69:in `measure’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
enchmarking.rb:69:in `perform_action_without_rescue' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/r escue.rb:82:in `perform\_action’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
ase.rb:381:in `send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/b ase.rb:381:in `process\_without\_filters’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/f  
ilters.rb:377:in `process_without_session_management_support' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/s ession_management.rb:117:in `process’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in  
 `dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:115:in `handle\_dispatch’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick\_server.rb:81:in  
 `service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service’  
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start\_thread’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start\_thread’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start’  
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:67:in `dispatch’  
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/webrick.  
rb:59  
c:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:18:in  
 `require__' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require’  
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/de  
pendencies.rb:147:in `require' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in `require_\_’  
c:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:18:in  
`require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/de pendencies.rb:147:in `require’  
script/server:3  
Request  
Parameters: {“id”=\>“1”}

Show session dump

* * *

flash: !ruby/hash:ActionController::Flash::FlashHash {}  
Response  
Headers: {“cookie”=\>[], “Cache-Control”=\>“no-cache”}

Regards,

Shreekar Joshi

---

<div class="post-metadata">

**Author:** ![Shreekar\_Joshi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Shreekar\_Joshi](https://www.ruby-forum.com/u/Shreekar_Joshi)\
**Post date:** [April 21, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/called-id-for-nil-which-would-mistakenly-be-4-if-you-rea/58417/2 "2006-04-21T14:32:13Z")

</div>

Hello Shreekar,

> ```
> I am a newbie to Ruby on Rails, but experienced in programming.
> 
> ```
> 
> I am trying out Rolling with Ruby on Rails article by Curt H. on  
> [onlamp.com](http://onlamp.com). I have encountered following error:
> 
> Called id for nil, which would mistakenly be 4 – if you really wanted  
> the id of nil, use object\_id
> 
> ```
> I searched the archives of the list and found that similar
> 
> ```
> 
> question was left unanswered. I do understand from the error that the  
> recipe.id is null(or nil).

Launch an irb and type :

nil.id

(irb):1: warning: Object#id will be deprecated; use Object#object\_id  
=\> 4

nil is an instance of the singleton class NilClass and nil is also  
an Object object, his object\_id is 4 (\*). So you’re calling Object#id (  
equivalent to Object#object\_id) method and not a AR::B#id to retrieve  
the primary key.

You have to test before than @recipe is not nil.

Maybe in your action controller, @recipe is not assigned correctly  
to be passed in your view.

```
-- Jean-FranÃ§ois.

```

---

<div class="post-metadata">

**Author:** ![Shreekar\_Joshi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Shreekar\_Joshi](https://www.ruby-forum.com/u/Shreekar_Joshi)\
**Post date:** [April 21, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/called-id-for-nil-which-would-mistakenly-be-4-if-you-rea/58417/3 "2006-04-21T19:44:50Z")

</div>

On Apr 21, 2006, at 5:03 AM, Shreekar Joshi wrote:

> I do understand from the error that the  
> recipe.id is null(or nil).

Actually, the error is telling you that recipe  
itself is nil, not recipe.id

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![Shreekar\_Joshi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Shreekar\_Joshi](https://www.ruby-forum.com/u/Shreekar_Joshi)\
**Post date:** [April 21, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/called-id-for-nil-which-would-mistakenly-be-4-if-you-rea/58417/4 "2006-04-21T14:53:39Z")

</div>

Hi Shreekar,

Shreekar Joshi wrote:

> I have encountered following error:

> Called id for nil, which would mistakenly  
> be 4 – if you really wanted the id of nil,  
> use object\_id
> 
> I have come to this page from edit link  
> from the show page. This show page is  
> generated by the scaffolding. The url for  
> this link _seems_ to be correct -  
> recipe/edit/2 (where 2 is id of the recipe).  
> Any ideas?

Two suggestions…

First, if you’re using InstantRails, a working version of the cookbook  
application is included and you can use that to check against the code  
you’re having problems with. If you’re not using InstantRails, you  
might  
want to download it from [http://instantrails.rubyforge.org/wiki/wiki.pl](http://instantrails.rubyforge.org/wiki/wiki.pl)

Second, to address your problem specifically, the error you’re getting  
results from trying to access an object in a view that was not  
initialized  
in the controller. Views typically use instance variables. So the  
problem  
usually traces back to something like…

recipe = Recipe.find(:all) # in this case, recipe is a local variable  
that the view sill not have access to  
@Recipe = Recipe.find(:all) # in this case, @Recipe is an instance  
variable, but it’s a different instance variable than @recipe.

There are lots of variations possible. Typos and capitaliation  
represent  
the largest proportion of errors I personally have made. If this  
doesn’t  
solve your problem, please post the controller code and I’m sure someone  
will be able to help.

Best regards,  
Bill
