【问题标题】:undefined method `key?' for nil:NilClass未定义的方法“键?”对于零:NilClass
【发布时间】:2012-03-31 02:48:38
【问题描述】:

我是 Rails 新手,正在关注 Ryan Bate 关于如何制作简单身份验证系统 (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 < 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,这可能是问题的一部分。但由于我是初学者,我不知道发生了什么。谁能告诉我该怎么做?

谢谢

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 有效!我只是不知道 Rails 有时需要重启。
    【解决方案2】:

    这是违规行:

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

    现在请仔细检查您是否按应输入的所有代码 :)

    【讨论】:

    • 感谢您的回答,但我仍然遇到同样的错误。感谢您提供堆栈溢出提示,我也是堆栈溢出的新手。
    • 另外,如果你偶然发现了 Q&A,你还需要将 gem 'bcrypt-ruby' 添加到你的 Gemfile,它位于你的 Rails 项目的顶级目录中。添加后,重新启动。
    【解决方案3】:

    您在 User 模型中的代码在密码上有拼写错误。

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

    应该是

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

    【讨论】:

      【解决方案4】:
        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
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 2012-03-15
          • 2016-06-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-31
          • 2014-11-21
          • 2016-11-21
          相关资源
          最近更新 更多