title: Rails API how do I send the active record validation error?
body: <p>I have model <code>User</code> with <code>validates_uniqueness_of :email</code>. In the front end (react), if I try to register with an address that's already taken, rails returns a 422 error. How can I instead send the error message to the front end?</p>
  
  <pre><code>class RegistrationsController &lt; ApplicationController
    def create
      user = User.create!(
        email: params['user']['email'],
        name: params['user']['name'],
        password: params['user']['password'],
        password_confirmation: params['user']['password_confirmation']
      )
      if user
        session[:user_id] = user.id
        render json: {
          status: :created,
          user: user
        }
      else
        render json: { status: 500 }
      end
    end
  end
  </code></pre>
  
  <p>React (axios):</p>
  
  <pre><code>.catch(error =&gt; {
          console.log("registration error", error);
        });
  </code></pre>
  
  <p>Console shows the error as <code>422 (Unprocessable Entity)</code>
  <a href="https://i.stack.imgur.com/0rdBj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0rdBj.png" alt="enter image description here"></a></p>
  
  <p>trying to log error.response doesn't show the activerecord error either
  <a href="https://i.stack.imgur.com/ATsyq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ATsyq.png" alt="enter image description here"></a></p>
  
  <p>Whereas in rails console I can see the error message
  <a href="https://i.stack.imgur.com/O4Ke1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O4Ke1.png" alt="enter image description here"></a></p>
  
  <p>Solved, the problem was using <code>create!</code><br>
  Managed to display the error nicely with</p>
  
  <p><code>render json: { errors: user.errors.full_messages }, status: 500</code>
  <code>error.response.data.errors.join("; ")</code></p>