使用 activemerchant 时将信用卡错误添加到“错误”中
我按照 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。 html 和 http://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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其添加到错误列表中和稍后显示该错误是两件不同的事情。
第一种方式看起来是正确的。这会将错误放入该对象的错误列表中(例如,可能还有其他验证错误。)
然后,您可以使用
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.
我也遇到了这个问题。我的解决方案有两个:
它最终看起来像这样:
这将错误放在正确的属性上,如果您使用像 simple_form 或 formtastic 这样的 gem,其中错误随其字段一起输出,这会特别有用,并且它会在基础上添加额外的错误,以防万一由于某种原因,映射不起作用。
I had a problem with this as well. My solution was twofold:
It ended up looking something like this:
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.