未定义的方法`key?’ 为零:NilClass

我是Rails的新手,正在关注Ryan Bate关于如何制作一个简单的身份validation系统的教程( http://railscasts.com/episodes/250-authentication-from-scratch?autoplay=true )我正在经历它但得到了这个错误:`

NoMethodError in UsersController#new undefined method `key?' for nil:NilClass Rails.root: C:/Sites/authentication` 

我真的不知道这意味着什么,因为我只是一个初学者,但这些是我的文件:

用户控制器:

 class UsersController  "Signed up!" else render "new" end end end 

new.html.erb:

    

Form is invalid

的routes.rb

  Authentication::Application.routes.draw do get "sign_up" => "users#new", :as => "sign_up" root :to => "users#new" resources :users end 

用户模型

 class User  create validates_presence_of :email validates_uniqueness_of :email def encrypt_password if password.present? self.password_salt = BCrypt::Engine.generate_salt self.password_hash = BCrypt::Engine.hash_secrete(password, password_salt) end end 

我认为该教程是针对Rails 3.1或某些版本的rails 3.但我使用的是Rails 3.2,这可能是问题的一部分。 但由于我是初学者,我不知道发生了什么。 有人能告诉我该怎么办?

谢谢

这是违规行:

 validates_presence_of :password, :on => create 

将其更改为

 validates_presence_of :password, :on => :create 

另外, 查看stackoverflow在您编写问题时向您显示的建议 。 阅读这些建议可以防止95%的问题。

更新

还有另一条线

 <%= form for @user do |f| %> 

应该

 <%= form_for @user do |f| %> 

现在请去三重检查您输入的所有代码:)

我有同样的问题,我的服务器的简单重启解决了它。

用户模型中的代码有秘密错字。

 self.password_hash = BCrypt::Engine.hash_secrete(password, password_salt) 

它应该是

 self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) 
  def encrypt_password if password.present? self.password_salt = BCrypt::Engine.generate_salt self.password_hash = BCrypt::Engine.hash_secrete(password, password_salt) end 

您还忘了为if语句添加结尾。 它应该是

  def encrypt_password if password.present? self.password_salt = BCrypt::Engine.generate_salt self.password_hash = BCrypt::Engine.hash_secrete(password, password_salt) end end 

我已经通过安装3.0.x版本的bcrypt-ruby来解决这个问题。 在Gemfile上指定它:

 gem 'bcrypt-ruby', '~> 3.0.0'