2

I am using stripe to setup a payment system. I am having an issue with my credit_card model displaying the stripe errors in my user model, as the first credit card is submitted as a child of the user. Also, I am using Devise for my users auth.

class CreditCard < ActiveRecord::Base

  def create_stripe_credit_card
    customer = Stripe::Customer.retrieve(self.user.stripe_customer_id)
    credit_card = customer.cards.create(:card => self.stripe_token)
    rescue Stripe::CardError => e
      logger.error "Stripe Error: " + e.message
      errors.add(:base, "#{e.message}")
      false
  end

end

class RegistrationsController < Devise::RegistrationsController

  def create
    build_resource(sign_up_params)

    if resource.save
      # User created
    else
      clean_up_passwords resource
      return render :status => 400, :json => resource.errors
    end
  end
end

After some googling I came upon this SO question

Ruby on Rails: how to get error messages from a child resource displayed?

but it seems that I would have to put a validator in my CreditCard model to trigger the errors in my User model, and since the only way to get this error raised is to try it, not sure how to put it into a validator.

Any help would be greatly appreciated.

Community
  • 1
  • 1
Romuloux
  • 1,027
  • 1
  • 8
  • 24

0 Answers0