【问题标题】:display braintree errors on fail失败时显示 Braintree 错误
【发布时间】:2016-06-29 10:46:06
【问题描述】:

您好,我正在尝试让我的 Braintree 帐户在创建交易时显示错误,但它似乎无法正常工作

- flash.each do |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 to #{params[:amount_to_add].to_f} to #{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 do |error|
        puts error.message
        customer.errors.add(:base, error.message)
        render :show, :notice => error.message
      end
    end
  end

【问题讨论】:

  • 我在终端中得到了响应,但我正在尝试解决如何将这些错误呈现回视图
  • 这并没有真正回答我的问题,我只是想将 Braintree 给我的错误返回到表示层
  • 不确定您是否遵循我真正需要的内容,以及为什么错误没有像应有的那样提供给表示层

标签: ruby-on-rails error-handling haml braintree


【解决方案1】:

我相信您可能看不到错误的原因是因为您的 render 方法上的 :notice 选项,这是多余的,因为 render 似乎没有使用 :notice 选项,仅redirect_to。您可以将错误添加到您的flash,但请注意,在您的视图中,您必须循环浏览 Flash 中的错误才能呈现它。

我认为您这样做的另一种方法是将付款方式添加到您的用户模型中

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 do |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 to #{params[:amount_to_add].to_f} to #{@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

在您看来,您可以访问@customer 变量,或者更好的是,您可以将错误对象存储在ActionController#flash 中,但是请注意,对您的闪存消息使用相同的键会覆盖之前的值。

【讨论】:

  • 认为这是在正确的轨道上,虽然 process_braintree_payment 给了我未定义的方法
  • 你从哪里得到这个错误?我的假设是您设置了 @customer 变量,否则您将尝试在 nil 对象上调用该方法
  • 这一切都在我的商店信用控制器/模型中完成,并为客户 def customer @customer ||= User.includes(:store_credit).find(current_user.id) end 提供自定义方法
猜你喜欢
  • 1970-01-01
  • 2012-08-10
  • 2013-04-28
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
相关资源
最近更新 更多