我一直在关注Michael Hartl的教程,对于那些熟悉它的人,我正在阅读第10课 - 更新用户。
到目前为止,我已经构建了一个应该允许用户注册,登录和注销的应用。我的应用允许用户注册,但在尝试登录时,我收到以下错误:
undefined method `-' for nil:NilClass
Rails.root: /Users/user/Sites/rails_projects/sample_app1
app/controllers/sessions_controller.rb:11:in `create'
{"utf8"=>"✓",
"authenticity_token"=>"/SEaqnrMf5X0pd4FVWbu8uWVAjTNw4LPKiXg+8Hl0PQ=",
"session"=>{"email"=>"Anthonypane@example.com",
"password"=>"[FILTERED]"},
"commit"=>"Sign in"}
这是我的Session_Controller内容:
class SessionsController < ApplicationController
def new
@title = "Sign in"
end
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title - "Sign In"
render 'new'
else
sign_in user
redirect_to user
end
end
def destroy
sign_out
redirect_to root_path
end
end
Users_Controller内容
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@title = @user.name
end
def new
@user = User.new
@title = "Sign up"
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
redirect_to @user, :flash => {:success => "Welcome to the Fun House!"}
else
@title = "Sign up"
render 'new'
end
end
end
Sessions_helper内容
module SessionsHelper
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
current_user = user
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= user_from_remember_token
end
def signed_in?
!current_user.nil?
end
def sign_out
cookies.delete(:remember_token)
self.current_user = nil
end
private
def user_from_remember_token
User.authenticate_with_salt(*remember_token)
end
def remember_token
cookies.signed[:remember_token] || [nil, nil]
end
end
最后Users_helper内容:
module UsersHelper
def gravatar_for(user, options = { :size => 50})
gravatar_image_tag(user.email.downcase, :alt => user.name,
:class => 'gravatar',
:gravatar => options)
end
end
答案 0 :(得分:1)
:-)感谢Taryn East解决了问题。
def has_password?(submitted_password)
encrypted_password == encrypted(submitted_password)
end
应该是
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
答案 1 :(得分:0)
问题是,与例外情况一样,您的行@title - "Sign In"
:它应该是
@title = "Sign In"
。
但是,我会说更大的问题是,当Rails 3.0 version是最新版本时,您现在正在执行教程的Rails 3.2 version。这绝对值得转换。