user3826864
user3826864

Reputation: 459

Rails - Capitalized Email Addresses cause errors no matter what I try

I have a relatively simple Rails app with a form using the Devise gem. The form does not require username but does require email address. Everything works fine and the page directs the user to the next page when their email address is all lowercase. However, when the user enters an email address with an uppercase character in the string, I receive the following error from Rails:

NoMethodError at /users
undefined method `uuid' for nil:NilClass

RegistrationsController#create
app/controllers/registrations_controller.rb, line 81

On line 81 (email: new_user.email,):

account         = Account.create(user: resource)
new_user        = User.find_by_email(params[:user][:email])
recurly_account = Recurly::Account.create(
        account_code: new_user.uuid,
        email:        new_user.email,
        first_name:   new_user.first_name,
        last_name:    new_user.last_name
)
sign_in new_user

Request Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"ySBGjHGNwiyeBqGh9nv0N5a8fIJO51bi1nIByev6a21Zh+ncMx5d99cTGKbKWpPvdYmGiBfLX9Gya0qmglrBWg==",
 "plan"=>"10day-fmf",
 "disc"=>"firstmonthfree10day",
 "user"=>{"first_name"=>"asdf", "last_name"=>"asdfsadf", "email"=>"[email protected]", "password"=>"lsdkjlfkjlskjdf"},
 "commit"=>"TRY RISK FREE!",
 "controller"=>"registrations",
 "action"=>"create"}

--note the email address with uppercase letters

I have researched this problem extensively and found that it has something to do with the email address not being downcased properly. Thus, based on my research I added the following code to various files:

# Devise.rb
config.case_insensitive_keys = [:email]
# User.rb
def downcase_email
  self.email.downcase!
end

#registrations_controller.rb
def check_email
  email = params[:email].downcase
  check = User.where(email: email).first

  begin
    check_recurly = Recurly::Account.find(email)

    if check_recurly
      recurly_email = "taken"
    end
  rescue Recurly::Resource::NotFound => e
    recurly_email = "available"
  end

However, despite all my attempts at downcasing the email addresses inputted by the user, uppercase letters still cause errors! Any help anyone could give me would be much appreciated! Thanks

Upvotes: 1

Views: 1290

Answers (3)

user3826864
user3826864

Reputation: 459

Although, I already marked the correct answer above, I wanted to mention that a little javascript onchange event also fixed the problem as well!

<%= f.input_field :email,  placeholder: "Email", id:"email", onchange: "this.value=this.value.toLowerCase();" %>

Upvotes: 1

Shuiei
Shuiei

Reputation: 47

Why are you checking manually if the email already exists? Devise normally do that for you. (did you specify the good user table name?

To lower the case in your controller to lower the params you can do

before_action :downcase_email, if: -> { params[:user][:email].present? }

def downcase_email
  params[:user][:email].downcase!
end

Btw you could refacto this method check_email into:

def check_email
  return unless params[:email].present?

  user = User.find_by_email(params[:email].downcase)
  return 'taken' if Recurly::Account.find(user)
  'available'
end

Something like that. You don't need to use begin/rescue

Upvotes: 0

sig
sig

Reputation: 392

Try to add .downcase here:
new_user = User.find_by_email(params[:user][:email].downcase)

I suppose that in database you have stored lowercase email value, but in controller in params[:user][:email] you have "[email protected]". Thats why User.find_by_email(params[:user][:email]) returns nil, and you cant call uuid method on a nil object.

Upvotes: 2

Related Questions