ruby on rails - display braintree errors on fail -
hi trying braintree account display errors when creating transaction doesn't appear working
- flash.each |key, value| %div{:class => "alert alert-#{key}"}= value def update result = braintree::transaction.sale( :amount => params[:amount_to_add].to_f, # :order_id => "order id", :customer_id => customer.customer_cim_id, :tax_amount => (params[:amount_to_add].to_f / 11).round(2), :options => { :submit_for_settlement => true } ) if result.success? logger.info "added #{params[:amount_to_add].to_f} #{customer.first_name} #{customer.last_name} (#{customer.customer_cim_id})" customer.store_credit.add_credit(params[:amount_to_add].to_f) redirect_to myaccount_store_credit_path # , :notice => "successfully updated store credit." else result.errors.each |error| puts error.message customer.errors.add(:base, error.message) render :show, :notice => error.message end end end
i believe reason you're unable see errors because of :notice
option on render
method, redundant because render
doesn't seem using :notice
option, redirect_to
. may add errors your flash
, note in view have loop through errors within flash render it.
another way think though add payment method user model
class user|customer ... def process_braintree_payment(amount) result = braintree::transaction.sale( :amount => amount.to_f, # :order_id => "order id", :customer_id => customer_cim_id, :tax_amount => (amount.to_f / 11).round(2), :options => { :submit_for_settlement => true } ) add_braintree_errors(result.error) unless result.success? end def add_braintree_errors(error_object) error_object.each |error| errors.add(:braintree, error.message) end end end class xcontroller def update @customer.process_braintree_payment(params[:amount_to_add]) if @customer.errors.empty? logger.info "added #{params[:amount_to_add].to_f} #{@customer.first_name} #{@customer.last_name} (#{@customer.customer_cim_id})" @customer.store_credit.add_credit(params[:amount_to_add].to_f) redirect_to myaccount_store_credit_path # , :notice => "successfully updated store credit." else render :show end end end
in view, have access @customer variable or better store error object in actioncontroller#flash
, note however, using same keys flash messages, overwrite previous value.
Comments
Post a Comment