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


#1

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. 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:insend’
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:inrender_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:inrender_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:inrender_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:inmeasure’
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:inperform_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:inperform_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:inperform_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:insend’
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:inprocess_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:indispatch’
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:inservice’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:94:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in each' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:instart’
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:inrequire__’
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:inrequire’
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:inrequire’
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:inrun_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:incompile_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:inrender_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:inrender_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:inrender’
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:inrender’
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:inperform_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:inmeasure’
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:inperform_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:inprocess_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:inprocess’
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:inhandle_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:inservice’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' c:/ruby/lib/ruby/1.8/webrick/server.rb:155:instart_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:instart_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:ineach’
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:instart’
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:indispatch’
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:inrequire’
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:inrequire
_’
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:inrequire’
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


#2

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

#3

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.


#4

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

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