title: How can I return custom errors to my form in Rails?
body: <p>I have a simple form on a website where a user enters a mailing address.</p>
  
  <p>I have a service which can validate this address and return various responses, either Success, Suspect, or Invalid, as well as return the full and most complete zip code for that address.  If the response is "Success", then I will save it to the db.  If the response is "Suspect", then I will update the zip field and ask them to confirm.  If the response is "Invalid" then I will return an error message asking them to contact us in person.</p>
  
  <p>I'm trying to set up my rails create action such that it makes a call to my service (for example http:/addresssValidator.com) and I want to inform the user if they have a valid address or not, and update the zip with the suggested zip code.</p>
  
  <p>Looking for address validation in rails however, seems to only give me APIs for using the built in error and validation system in rails and not how to return my own custom results to the form.</p>
  
  <p>How can I do this?  </p>
  
  <p>Below is my code:</p>
  
  <pre><code>def create
    @valid = validate_address
      @address = Address.new(address_params)
    if @valid
      if @address.save
        redirect_to "/survey/success"
      else
        p @address.errors
        respond_to do |format|
          format.json {render json: @address.errors}
          format.html {render "/survey/failure"}
  
        end
  
      end
    else
      ##//Display errors
    end
  
    end 
  
  def validate_address
  
  @api_key = "my_api_key"
  HTTParty.post("http://addressValidator.com",
                {
                    :body =&gt; [
                        "StreetAddress" =&gt;  params[:address][:address_line_2] + " " + params[:address][:address_line_1],
                        "City" =&gt; params[:address][:city],
                        "PostalCode" =&gt; params[:address][:zip],
                        "State" =&gt; params[:address][:state],
                        "CountryCode" =&gt; params[:address][:country],
                        "Locale" =&gt; "en" ].to_json,
                    :APIKey =&gt; @api_key ,
                    :headers =&gt; { 'Content-Type' =&gt; 'application/json', 'Accept' =&gt; 'application/json'}
                })
  
  
  return false;
  ##//return actual results from validator
    end
  </code></pre>