我已经构建了一个应用程序,允许用户注册、登录和退出(如果你愿意的话,我自己的设计是从头开始的)。
该应用程序允许用户注册,但在尝试登录时出现以下错误:
//(不用说我处于静止状态,谁能弄清楚这就是他们自己的天才)
SessionsController#create 中的 NoMethodError
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