未定义的方法“键?"对于nil:NilClass [英] undefined method `key?' for nil:NilClass

查看:92
本文介绍了未定义的方法“键?"对于nil:NilClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails的新手,并且正在关注Ryan Bate关于如何制作简单身份验证系统的教程(

I'm new to Rails and was following Ryan Bate's tutorial on how to make a simple authentication system (http://railscasts.com/episodes/250-authentication-from-scratch?autoplay=true) and I was just going through it but got this error: `

NoMethodError in UsersController#new

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

由于我只是一个初学者,所以我真的不知道这意味着什么,但这是我的文件:

I don't really know what this means since I'm just a beginner, but these are my files:

用户控制器:

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:

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

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

I think the tutorial was made for Rails 3.1 or some version of rails 3. But I am using Rails 3.2, that might be part of the problem. But since I'm a beginner I have no idea what is going on. Could someone tell me what to do?

谢谢

推荐答案

这是令人反感的行:

validates_presence_of :password, :on => create

将其更改为

validates_presence_of :password, :on => :create

看看

Also, do look at suggestions that stackoverflow shows you when you write a question. Reading those suggestions prevents 95% of my questions.

还有另一行

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

应该是

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

现在,请仔细检查您是否键入了所有代码:)

Now please go and triple check that you typed all the code as you should :)

这篇关于未定义的方法“键?"对于nil:NilClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆