Pavan Kumar Pavan Kumar - 1 month ago 7
Ruby Question

undefined method `[]' for nil:NilClass + after login

I'm getting this error after hitting login
my full trace is

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

Rails.root: /home/enroco/development/enrocart

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"=>"b7t1yJ7cGyxDmecBpvBrGLRbh/ubtPjsUFdP/HosK8kp/ZLRtrV3dVWE2vPjDnThCbt9hk1ftm5ZMg9Ia1StWw==",
"email"=>{"{:placeholder=>\"email@address.com\",
:tabIndex=>\"1\",
\"class\"=>\"\"}"=>"pavanez4u@gmail.com"},
"password"=>"[FILTERED]",
"commit"=>"Log In"}


My sessions controller file is

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 file is

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


Working on ruby 1.9.3, rails 4.2.5
I'm unable to login after hitting login button, even signup form is also getting this kind of issue. Help out me to fix this issue?

Answer

The parameters you are sending are incorrect, you are taking the params value as a string, which is incorrect,

Try sending params as key and value,

Send Parameters as:

{

    "utf8"=>"✓",
  "authenticity_token"=>"b7t1yJ7cGyxDmecBpvBrGLRbh/ubtPjsUFdP/HosK8kp/ZLRtrV3dVWE2vPjDnThCbt9hk1ftm5ZMg9Ia1StWw==",
    "email"=>{"pavanez4u@gmail.com",
    "tabIndex"=>"1",
    "password"=>"[FILTERED]"
    "commit"=>"Log In"
}

@email = params[:email] this line throws you an error because the params format is incorrect.

Comments