使用 activemerchant 时将信用卡错误添加到“错误”中

发布于 2024-08-17 00:52:18 字数 761 浏览 4 评论 0原文

我按照 activemerchant 上的railscast 进行操作,并得到了以下代码:

def validate_card
  unless credit_card.valid?
    credit_card.errors.full_messages.each do |message|
      errors.add_to_base message
    end
  end
end

但这并没有将字段包装在 fieldWithErrors div 中。所以我尝试了:

def validate_card
  unless credit_card.valid?
    credit_card.errors.each do |error|
      errors.add error
    end
  end
end

那仍然不起作用。我读过 http://api.rubyonrails.org/classes/ActiveResource/Errors。 htmlhttp://activemerchant.rubyforge.org/ 但我没有阅读它们对或什么的。

I followed the railscast on activemerchant and have this code:

def validate_card
  unless credit_card.valid?
    credit_card.errors.full_messages.each do |message|
      errors.add_to_base message
    end
  end
end

But that doesn't wrap the field in a fieldWithErrors div. So I tried:

def validate_card
  unless credit_card.valid?
    credit_card.errors.each do |error|
      errors.add error
    end
  end
end

That still didn't work. I've read http://api.rubyonrails.org/classes/ActiveResource/Errors.html and http://activemerchant.rubyforge.org/ but I'm not reading them right or something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半﹌身腐败 2024-08-24 00:52:18

将其添加到错误列表中和稍后显示该错误是两件不同的事情。

第一种方式看起来是正确的。这会将错误放入该对象的错误列表中(例如,可能还有其他验证错误。)

然后,您可以使用 error_messages_for() (api ref) 在您的视图中输出该错误。它可以根据您想要的 div 名称进行定制。

或者,您可以通过自己循环 @object.errors 来完成自己的输出。

Adding it to the list of errors and showing that error(s) later are two different things.

The first way you've got it looks right. That puts the error into the list of errors for this object (there might be other validation errors, for instance.)

Then, you can use error_messages_for() (api ref) to output that error in your view. It's customizable for whatever you want to call your divs.

Or, you can do your own output by looping through @object.errors yourself.

梦旅人picnic 2024-08-24 00:52:18

我也遇到了这个问题。我的解决方案有两个:

  1. 将一般的“信用卡无效”错误添加到基础
  2. 将每个信用卡错误映射到我自己的模型的属性。

它最终看起来像这样:

def valid_credit_card
  unless credit_card.valid?
    errors.add(:base, 'Credit card is invalid') # optional
    credit_card.errors.each do |attr, messages|
      # Map to the model properties, assuming you used the 
      # setup from the Railscast
      if attr =~ /month|year/
        attr = 'card_expires_on'
      elsif attr =~ /(first|last)_name/
        # attr = attr
      else
        attr = "card_#{attr}".gsub(/_value/, '')
      end
      messages.each { |m| errors.add(attr, m) unless errors[attr].include?(m) }
    end
  end
end

这将错误放在正确的属性上,如果您使用像 simple_form 或 formtastic 这样的 gem,其中错误随其字段一起输出,这会特别有用,并且它会在基础上添加额外的错误,以防万一由于某种原因,映射不起作用。

I had a problem with this as well. My solution was twofold:

  1. Add a general 'Credit card is invalid' error to base
  2. Map each credit card error to my own model's attributes.

It ended up looking something like this:

def valid_credit_card
  unless credit_card.valid?
    errors.add(:base, 'Credit card is invalid') # optional
    credit_card.errors.each do |attr, messages|
      # Map to the model properties, assuming you used the 
      # setup from the Railscast
      if attr =~ /month|year/
        attr = 'card_expires_on'
      elsif attr =~ /(first|last)_name/
        # attr = attr
      else
        attr = "card_#{attr}".gsub(/_value/, '')
      end
      messages.each { |m| errors.add(attr, m) unless errors[attr].include?(m) }
    end
  end
end

This puts the errors on the proper attributes, which is especially helpful if you're using gems like simple_form or formtastic where the errors are output with their fields, and it adds the extra error on base in case for some reason the mappings don't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文