我在登录后遇到此错误 我的完整痕迹是
undefined method `[]' for nil:NilClass
Extracted source (around line #20):
def create
@session = Session.new(params[:session], request.remote_ip)
if @session.valid?
set_current_user(@session.current_user)
add_to_current_cart params
Application Trace | Framework Trace | Full Trace
app/models/session.rb:12:in `initialize'
app/controllers/sessions_controller.rb:20:in `new'
app/controllers/sessions_controller.rb:20:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>==",
"email"=>{"{:placeholder=>\"email@address.com\",
:tabIndex=>\"1\",
\"class\"=>\"\"}"=>""},
"password"=>"[FILTERED]",
"commit"=>"Log In"}
我的会话控制器文件是
def create
@session = Session.new(params[:session], request.remote_ip)
if @session.valid?
set_current_user(@session.current_user)
add_to_current_cart params
add_to_favorites params
if session[:return_to]
redirect_to session[:return_to]
else
Session.rb文件是
class Session
INCORRECT_PASSWORD = "The password you entered is incorrect. Please try again (make sure your caps lock is off)."
EMAIL_NOT_FOUND = "The email id you entered does not belong to any account."
MANDATORY_FIELDS = "You must fill in all of the fields"
include ActiveModel::Validations
attr_accessor :email, :password, :errors
attr_reader :current_user
def initialize(params={}, ip_address=nil)
@errors = ActiveModel::Errors.new(self)
@email = params[:email]
@password = params[:password]
@current_user = User.find_by_email(@email)
@ip_address = ip_address
end
def valid?
mandatory_fields_presence
if @current_user && @errors.empty?
if @current_user.waiting_for_confirmation?
email_confirmation_link = "/account/#{@current_user.uuid}/send-confirmation-email"
@errors.add(:base, "Please confirm your account first. <a href='#{email_confirmation_link}'>Resend confirmation link</a>")
end
end
authenticate if @errors.empty?
@current_user.signed_in(@ip_address) if @errors.empty?
@errors.empty?
end
private
def mandatory_fields_presence
@errors.add(:email, MANDATORY_FIELDS) if @email.blank? or @password.blank?
end
def authenticate
@errors.add(:email, EMAIL_NOT_FOUND) if @current_user.nil?
if @errors.empty? && !@current_user.authenticated?(@password)
@errors.add(:password, INCORRECT_PASSWORD)
end
end
end
if session[:login_page] == true
redirect_to root_path
else
redirect_to :back
end
end
else
if session[:login_page] == true
render :new
else
flash[:login_failed] = OpenStruct.new({ :errors => @session.errors, :login_failed => true })
redirect_to :back
end
end
end
使用ruby 1.9.3,rails 4.2.5 我点击登录按钮后无法登录,即使注册表格也出现了这种问题。帮我解决这个问题?
答案 0 :(得分:1)
您发送的参数不正确,您将params值作为字符串,这是不正确的,
尝试将params作为键和值发送,
发送参数:
{
"utf8"=>"✓",
"authenticity_token"=>"b7t1yJ7cGyxDmecBpvBrGLRbh/ubtPjsUFdP/HosK8kp/ZLRtrV3dVWE2vPjDnThCbt9hk1ftm5ZMg9Ia1StWw==",
"email"=>{"pavanez4u@gmail.com",
"tabIndex"=>"1",
"password"=>"[FILTERED]"
"commit"=>"Log In"
}
@email = params[:email]
此行会引发错误,因为params格式不正确。