title: Check validation before save rails
body: <p>Using Stripe and want to: 
   1. check model validation 
   2. if no errors process card token. 
   3. If card works then save new record.</p>
  
  <p>Below I have it where it processes card first, then if successful, saves record.  Unfortunately that record might fail validation but the card was already charged.</p>
  
  <p>As a hack I have a js validation (that mirrors the model validations) in the view that doesn't allow the form to be submitted until the terms are met.  </p>
  
  <p>Tourregistration.rb Controller</p>
  
  <pre><code>    def create
  token = params[:tourregistration][:stripeToken]
  begin
    charge = Stripe::Charge.create(
    :amount =&gt; (params[:tourregistration][:invoice].to_d*100).round,
    :currency =&gt; "usd",
    :source =&gt; token,
    :description =&gt; "Example charge"
    )
  rescue Stripe::CardError =&gt; e
    # The card has been declined
    redirect_to :back, :notice =&gt; "#{e}"
  else
  
  @tourregistrations = tourregistration_data.map do |tourregistration_params|
    Tourregistration.new(tourregistration_params)
  end
  
  @tourregistrations.each(&amp;:save)
  
    if @tourregistrations.all?(&amp;:valid?)
      flash[:notice] = 'Sign-up Complete'
      redirect_to root_url
    else
      redirect_to :back, :notice =&gt; "Something went wrong. Please fill in all fields."  
    end    
  end
  </code></pre>
  
  <p>end</p>
  
  <p>Tourregistration.rb Model</p>
  
  <pre><code>class Tourregistration &lt; ActiveRecord::Base
  belongs_to :patron
  belongs_to :tour
  has_one :referrer
  
  validates :fname, length: { minimum: 2}
  validates :lname, length: { minimum: 2}
  validates :email, presence: true
  validates :price, presence: true
  validates :invoice, presence: true
  validates :patron_id, presence: true
  validates :tour_id, presence: true
  end
  </code></pre>