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

时间:2012-03-31 02:48:39

标签: ruby-on-rails authentication ruby-on-rails-3.2 rails-activerecord railscasts

我是Rails的新手,正在关注如何创建一个简单的身份验证系统(http://railscasts.com/episodes/250-authentication-from-scratch?autoplay=true)的Ryan Bate的教程,我刚刚经历过这个错误:`

NoMethodError in UsersController#new

undefined method `key?' for nil:NilClass
Rails.root: C:/Sites/authentication`

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

用户控制器:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
        redirect_to root_url, :notice => "Signed up!"
    else
        render "new"
    end
  end
end

new.html.erb:

    <%= form for @user do |f| %>
<% if @user.errors.any? %>
<div class="error_messages">
    <h2>Form is invalid</h2>
    <ul>
        <% for message in @user.errors.full_messages %>
        <li><%= message %></li>
        <% end %>
    </ul>
</div>
<% end %>
<p>
    <%= f.label :email %>
    <%= f.text_field :email %>
</p>
<p>
    <%= f.label :password %>
    <%= f.password_field :password %>
</p>
<p>
    <%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation %>
</p>
<p class="button"><%= f.submit %></p>
<% end %>

的routes.rb

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

用户模型

class User < ActiveRecord::Base
    attr_accessor :password
    before_save :encrypt_password

    validates_confirmation_of :password
    validates_presence_of :password, :on => 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,这可能是问题的一部分。但由于我是初学者,我不知道发生了什么。有人能告诉我该怎么做吗?

由于

5 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:8)

这是违规行:

validates_presence_of :password, :on => create

将其更改为

validates_presence_of :password, :on => :create

此外,当您撰写问题时, 执行 会查看suggestions that stackoverflow shows you。阅读这些建议可以防止95%的问题。

更新

还有另一条线

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

应该是

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

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

答案 2 :(得分:1)

您在用户模型中的代码中存在错误信息。

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

应该是

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

答案 3 :(得分:1)

  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

答案 4 :(得分:0)

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

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