UsersController中的NoMethodError #show / undefined方法`key?'为零:NilClass

时间:2012-10-09 01:08:43

标签: ruby-on-rails railstutorial.org

我在跟随Michael Hartl的 Rails教程时遇到了意外错误。一帆风顺。

我第一次看到我得到了一个不同的"动作控制器:异常被捕获"完成Hartl's Listing 7.3后的错误比教程错误。

具体来说,当试图达到/ users / 1时,我得到:

  

UsersController#show中的NoMethodError

     

未定义的方法`密钥?'为零:NilClass

我继续阅读清单7.5 - 为用户的show动作添加一个视图,然后将show动作添加到Users控制器 - 这解决了Hartl得到的错误,但这些步骤并没有解决我的错误。

根据this question,我已经确认我的app / models / user.rb没有拼写错误:在搜索拼写错误后,我复制了&将Hartl的代码粘贴到我的文件中以便进行测量。

非常感谢任何关于在何处寻找拼写错误/问题的指导。

编辑1:根据要求,UsersController #show方法:

def show
  @user = User.find(params[:id])
end

编辑2:完整跟踪:

  

actionpack(3.2.8)lib / action_controller / metal / hide_actions.rb:36:在`visible_action?'

     

actionpack(3.2.8)lib / action_controller / metal / hide_actions.rb:18:在`method_for_action'

     

actionpack(3.2.8)lib / action_controller / metal / implicit_render.rb:14:在`method_for_action'

     

actionpack(3.2.8)lib / action_controller / metal / compatibility.rb:61:在`method_for_action'

     

actionpack(3.2.8)lib / abstract_controller / base.rb:115:在`process'

     

actionpack(3.2.8)lib / abstract_controller / rendering.rb:45:在`process'

     

actionpack(3.2.8)lib / action_controller / metal.rb:203:在`dispatch'

     

actionpack(3.2.8)lib / action_controller / metal / rack_delegation.rb:14:在`dispatch'

     

actionpack(3.2.8)lib / action_controller / metal.rb:246:在`block in action'

     

actionpack(3.2.8)lib / action_dispatch / routing / route_set.rb:73:在`call'

     

actionpack(3.2.8)lib / action_dispatch / routing / route_set.rb:73:在`dispatch'

     

actionpack(3.2.8)lib / action_dispatch / routing / route_set.rb:36:在`call'

     

旅程(1.0.4)lib / journey / router.rb:68:在`阻止通话'

     

旅程(1.0.4)lib / journey / router.rb:56:在每个'

     

旅程(1.0.4)lib / journey / router.rb:56:在`call'

     

actionpack(3.2.8)lib / action_dispatch / routing / route_set.rb:600:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / best_standards_support.rb:17:在`call'

     

rack(1.4.1)lib / rack / etag.rb:23:在`call'

     

rack(1.4.1)lib / rack / conditionalget.rb:25:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / head.rb:14:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / params_parser.rb:21:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / flash.rb:242:在`call'

     

rack(1.4.1)lib / rack / session / abstract / id.rb:205:在`context'

     

rack(1.4.1)lib / rack / session / abstract / id.rb:200:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / cookies.rb:339:在`call'

     

activerecord(3.2.8)lib / active_record / query_cache.rb:64:在`call'

     

activerecord(3.2.8)lib / active_record / connection_adapters / abstract / connection_pool.rb:473:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / callbacks.rb:28:in block in call'

     

activesupport(3.2.8)lib / active_support / callbacks.rb:405:在`_run__35606540​​18285941260__call__3098371293035639072__callbacks'

     

activesupport(3.2.8)lib / active_support / callbacks.rb:405:在`__run_callback'

     

activesupport(3.2.8)lib / active_support / callbacks.rb:385:在`_run_call_callbacks'

     

activesupport(3.2.8)lib / active_support / callbacks.rb:81:在`run_callbacks'

     

actionpack(3.2.8)lib / action_dispatch / middleware / callbacks.rb:27:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / reloader.rb:65:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / remote_ip.rb:31:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / debug_exceptions.rb:16:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / show_exceptions.rb:56:在`call'

     

railties(3.2.8)lib / rails / rack / logger.rb:26:在`call_app'

     

railties(3.2.8)lib / rails / rack / logger.rb:16:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / request_id.rb:22:在`call'

     

rack(1.4.1)lib / rack / methodoverride.rb:21:在`call'

     

rack(1.4.1)lib / rack / runtime.rb:17:在`call'

     

activesupport(3.2.8)lib / active_support / cache / strategy / local_cache.rb:72:在`call'

     

rack(1.4.1)lib / rack / lock.rb:15:在`call'

     

actionpack(3.2.8)lib / action_dispatch / middleware / static.rb:62:在`call'

     

railties(3.2.8)lib / rails / engine.rb:479:在`call'

     

railties(3.2.8)lib / rails / application.rb:223:在`call'

     

rack(1.4.1)lib / rack / content_length.rb:14:在`call'

     

railties(3.2.8)lib / rails / rack / log_tailer.rb:17:在`call'

     

rack(1.4.1)lib / rack / handler / webrick.rb:59:在`service'

     

/Users/aaronmacy/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in`service'

     

/Users/aaronmacy/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in“run'

     

/Users/aaronmacy/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in,clock in start_thread'

编辑3: users_controller.rb:

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def new
  end
end

user.rb:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password

  before_save { self.email.downcase! }

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
end

routes.rb中:

SampleApp::Application.routes.draw do
  resources :users

  root to: 'static_pages#home'

  match '/signup',  to: 'users#new'

  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'
end

1 个答案:

答案 0 :(得分:1)

这是一个bcrypt问题。验证您的Gemfile中是否有未注释的bcrypt,运行bundle install,然后重新启动服务器。

请参阅:undefined method `key?' for nil:NilClass with bcrypt-ruby and has_secure_password