Checking if the email had been already taken in the following if statement (Rails)?

Posted by alexchenco on Stack Overflow See other posts from Stack Overflow or by alexchenco
Published on 2012-11-06T04:23:10Z Indexed on 2012/11/06 5:00 UTC
Read the original article Hit count: 130

I have the following action:

users.rb:

  def omniauth_create
    auth = request.env["omniauth.auth"]
    user = User.from_omniauth(env["omniauth.auth"])
    unless user.email.blank?
      if user.id.nil?
        # Save the user since he hasn't been created yet
        user.save!
      end
      sign_in user
      redirect_back_or user
    else
      # Send user to a form to fill his email
      #session[:omniauth] = request.env['omniauth.auth'].except('extra')
      redirect_to(enter_email_path(oprovider: user.provider,
                                   ouid: user.uid,
                                   oname: user.name,
                                   opassword: user.password,
                                   opassword_confirmation: user.password))
    end
  end

It does the following:

  • If the user's email is not blank, sign him in, and redirect him to his profile (and save him if his id is nil. In other words, if he hasn't been created yet).
  • If the user's email is blank, send him to enter_email_path (where the user can enter his email).

Now I want to add another if statement that flashes an error if the email had been already taken, and redirects the user to the root_path

I'm not very sure how to do this, Any suggestions? (and where to put that if statement?)

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby