0

I have a form where the user can add email addresses separated by a ','.

I process the emails in the controller:

        email_arr = extract_emails  
    email_arr.each do |email|
      unless @user.save 
      @email_err << email
      @user.add_email_errors
    end

In the User model:

def add_email_errors errors.add("#{email} is not a valid email address") end

On the form:

<%= error_messages_for :user %>

All this works fine except I am only getting the error message for the last email address. So if a user enters the following:

bademail.com, notvalidemail.ca, etc on the form.

The error message is: "notvalidemail.ca is not a valid email address"

How can I get a list of all the invalid emails?

Thank you in advance.

1 Answer 1

1

If I remember correctly, save resets the error table.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I took out the @user.save and replaced it with @user.valid? I still just get the last bad email thought.
actually, more specifically, I think valid? resets it. You may have to keep track of the errors in another variable and then add them at the end after the loop
Ok. Thanks. I guess that is the best way. I thought I could just keep adding them to the errors.add :email method and then get them all back on the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.