开发者

NoMethodError in Users#new

开发者 https://www.devze.com 2023-04-07 19:23 出处:网络
I started getting this error: NoMethodError: undefined method `has_key?\' for nil:NilClass in the process of nearly finishing Hartl\'s excellent Rails Tutorial. It looks like I must have left out a

I started getting this error:

NoMethodError: undefined method `has_key?' for nil:NilClass

in the process of nearly finishing Hartl's excellent Rails Tutorial. It looks like I must have left out a method, but I have idea where, and am new to debugging in Rails.

Here's the full stack trace:

activerecord (3.0.9) lib/active_record/attribute_methods/read.rb:80:in `id'
activerecord (3.0.9) lib/active_record/attribute_methods/primary_key.rb:9:in `to_key'
actionpack (3.0.9) lib/action_controller/record_identifier.rb:82:in `record_key_for_dom_id'
actionpack (3.0.9) lib/action_controller/record_identifier.rb:63:in `dom_id'
actionpack (3.0.9) lib/action_view/helpers/form_helper.rb:331:in `apply_form_for_options!'
actionpack (3.0.9) lib/action_view/helpers/form_helper.rb:313:in `form_for'
app/views/users/new.html.erb:3:in `_app_vi开发者_C百科ews_users_new_html_erb___834068491__620833808_0'
actionpack (3.0.9) lib/action_view/template.rb:135:in `send'
actionpack (3.0.9) lib/action_view/template.rb:135:in `render'
activesupport (3.0.9) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.9) lib/action_view/template.rb:127:in `render'
actionpack (3.0.9) lib/action_view/render/rendering.rb:59:in `_render_template'
activesupport (3.0.9) lib/active_support/notifications.rb:52:in `instrument'
activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.9) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.9) lib/action_view/render/rendering.rb:56:in `_render_template'
actionpack (3.0.9) lib/action_view/render/rendering.rb:26:in `render'
actionpack (3.0.9) lib/abstract_controller/rendering.rb:115:in `_render_template'
actionpack (3.0.9) lib/abstract_controller/rendering.rb:109:in `render_to_body'
actionpack (3.0.9) lib/action_controller/metal/renderers.rb:47:in `render_to_body'
actionpack (3.0.9) lib/action_controller/metal/compatibility.rb:55:in `render_to_body'
actionpack (3.0.9) lib/abstract_controller/rendering.rb:102:in `render_to_string'
actionpack (3.0.9) lib/abstract_controller/rendering.rb:93:in `render'
actionpack (3.0.9) lib/action_controller/metal/rendering.rb:17:in `render'
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:40:in `render'
activesupport (3.0.9) lib/active_support/core_ext/benchmark.rb:5:in `ms'
/usr/lib/ruby/1.8/benchmark.rb:308:in `realtime'
activesupport (3.0.9) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
activerecord (3.0.9) lib/active_record/railties/controller_runtime.rb:15:in `cleanup_view_runtime'
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.0.9) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.0.9) lib/action_controller/metal/mime_responds.rb:261:in `retrieve_response_from_mimes'
actionpack (3.0.9) lib/action_controller/metal/mime_responds.rb:192:in `call'
actionpack (3.0.9) lib/action_controller/metal/mime_responds.rb:192:in `respond_to'
app/controllers/users_controller.rb:29:in `new'
actionpack (3.0.9) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.9) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.9) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.9) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.9) lib/abstract_controller/callbacks.rb:18:in `process_action'
activesupport (3.0.9) lib/active_support/callbacks.rb:436:in `_run__434435962__process_action__943997142__callbacks'
activesupport (3.0.9) lib/active_support/callbacks.rb:410:in `send'
activesupport (3.0.9) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks'
activesupport (3.0.9) lib/active_support/callbacks.rb:94:in `send'
activesupport (3.0.9) lib/active_support/callbacks.rb:94:in `run_callbacks'
actionpack (3.0.9) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
activesupport (3.0.9) lib/active_support/notifications.rb:52:in `instrument'
activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.9) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.9) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.9) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.9) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.9) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.9) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.9) lib/action_controller/metal.rb:178:in `action'
actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:103:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:493:in `call'
actionpack (3.0.9) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.9) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.3) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.9) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.9) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.9) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.9) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.9) lib/active_record/query_cache.rb:32:in `call'
activerecord (3.0.9) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.9) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.9) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:46:in `call'
activesupport (3.0.9) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.3) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.9) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.9) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.9) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.3) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.3) lib/rack/lock.rb:11:in `call'
rack (1.2.3) lib/rack/lock.rb:11:in `synchronize'
rack (1.2.3) lib/rack/lock.rb:11:in `call'
actionpack (3.0.9) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.9) lib/rails/application.rb:168:in `call'
railties (3.0.9) lib/rails/application.rb:77:in `send'
railties (3.0.9) lib/rails/application.rb:77:in `method_missing'
railties (3.0.9) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.3) lib/rack/content_length.rb:13:in `call'
rack (1.2.3) lib/rack/handler/webrick.rb:52:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
rack (1.2.3) lib/rack/handler/webrick.rb:13:in `run'
rack (1.2.3) lib/rack/server.rb:217:in `start'
railties (3.0.9) lib/rails/commands/server.rb:65:in `start'
railties (3.0.9) lib/rails/commands.rb:30
railties (3.0.9) lib/rails/commands.rb:27:in `tap'
railties (3.0.9) lib/rails/commands.rb:27
script/rails:6:in `require'
script/rails:6

I can see that the error is because a User is not getting created during the signin process. So I went into the console, and just tried to create a User; here's the error stack from that:

from script/rails:6irb(main):004:0> user=User.new
NoMethodError: undefined method `has_key?' for nil:NilClass
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.9/lib/active_support/whiny_nil.rb:48:in `method_missing'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/active_record/base.rb:1512:in `has_attribute?'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/active_record/base.rb:1672:in `inspect'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/active_record/base.rb:1671:in `collect'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.9/lib/active_record/base.rb:1671:in `inspect'
    from /usr/lib/ruby/1.8/irb.rb:310:in `output_value'
    from /usr/lib/ruby/1.8/irb.rb:159:in `eval_input'
    from /usr/lib/ruby/1.8/irb.rb:271:in `signal_status'
    from /usr/lib/ruby/1.8/irb.rb:155:in `eval_input'
    from /usr/lib/ruby/1.8/irb.rb:154:in `eval_input'
    from /usr/lib/ruby/1.8/irb.rb:71:in `start'
    from /usr/lib/ruby/1.8/irb.rb:70:in `catch'
    from /usr/lib/ruby/1.8/irb.rb:70:in `start'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in `start'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in `start'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:23
    from script/rails:6:in `require'

I'm using Rails 3.0.9. If anyone can point me in the right direction, sure would appreciate it!

thanks,

rick

Additional post (9/27/11):

to help my commenters --

yes, I've migrated my database.

where I'm at in the Tutorial: I've basically finished the tutorial, but differed from how it coded up Users, but using a scaffold instead of the incremental approach that Hartl takes.

here's users/new.html.erb:

<h1>New user</h1>

<%= form_for(@user) do |f| %>
  <div class="field">
    <%= f.label :firstname, "First Name" %><br />
    <%= f.text_field :firstname %>
  </div>
  <div class="field">
    <%= f.label :lastname, "Last Name" %><br />
    <%= f.text_field :lastname %>
  </div>
  <div class="field">
    <%= f.label :username %><br />
    <%= f.text_field :username %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation, "Confirmation" %><br />
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>


<%= link_to 'Back', users_path %>

Where I think is causing my bug is that a user object does not get created by the model, for some reason; and that's why I get the 'nil object' error.

Any help much appreciated!

--rick


Welcome to Rails and to debugging a Rails program.

Some general debugging tips.

  • While the nil object does have a number of methods, it is obviously not an ActiveRecord object and thus doesn't have the has_key? method.

  • So the problem (as you surmised) is that the method that should return an ActiveRecord object is returning nil instead.

  • So next is to figure out why. Since you're trying to create the ActiveRecord object it may well be that the object is not passing its validations. Check this by using valid? before saving. Or use save! instead of save The save! method will throw an error if the save operation doesn't succeed.

  • Learn how to use the logging statement to send log messages to the log file as you debug.

  • You can also use the interactive debugger. It can be set up so it'll be invoked when the browser's request reaches a particular line of code in the server. -- The browser will sit with an hour-glass until you "continue" within the debugger.

Added Debugging validations

To debug validations, I use a couple of techniques:

  • Don't try to use the debugger to follow the execution of the validations--as you've seen, that's a mug's game.
  • Do use the debugger to invoke the save or valid? method on the AR object. You can then inspect the object or use a method to view the error object that is contained within the AR object. The error object will often tell you which validation failed.
  • You can also individually comment out validations until you find the one that is giving you a problem.
  • You can also use the debugger to examine the attributes of the AR object to see whether the attribute values will pass validation or not.
  • Re: when do validations run (as you ask in your comment). Do remember that by default, validations will run on create as well as update calls. If you want an individual validation to only be checked on update (not on create), there are several ways to do that, check the docs for the individual validation.

There's more to say about validations. If you have a specific validation that fails and you don't understand why it is failing, then open a separate question in SO.

Re: why do validations have to be passed on creation, not just updates -- that's a feature. It enables you to assure yourself that only valid objects are being created in the db. But you don't have to use it. You can set validations to only be active on Update.

Validations are a powerful, sophisticated part of ActiveRecord. In other words, they can be complicated until they're understood.


Maybe on your User model you were creating an initialize method that overrode ActiveRecord:Base

script/console

class User < ActiveRecord:Base

  def initialize

  end
end

User.new
=> NoMethodError: undefined method `has_key?' for nil:NilClass
0

精彩评论

暂无评论...
验证码 换一张
取 消