私は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 %>
ルート.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
チュートリアルはRails3.1またはRails3の一部のバージョン用に作成されたと思います。しかし、私はRails 3.2を使用していますが、これは問題の一部である可能性があります。しかし、私は初心者なので、何が起こっているのかわかりません。誰かが私に何をすべきか教えてもらえますか?
ありがとう