我已经构建了一个应用程序,允许用户注册、登录和注销(如果你愿意的话,我自己从头开始设计)。
该应用程序允许用户注册,但当尝试登录时,我会收到以下错误:
//
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
用户_控制器内容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
会话帮助程序内容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
以及Last 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
我认为这一行:
@title - "Sign In"
应该是这样的:
@title = "Sign In"