Friday, 15 April 2011

Rails: Credit Card Validator gem - need good example of how to use it -



Rails: Credit Card Validator gem - need good example of how to use it -

i looking utilize credit card validator gem validate credit card numbers , types. i'm still relatively new rails , though i've taken @ github documentation still trying work out how best utilize gem. need set code model? if so, how exactly? need utilize validate_with?

i've done few google searches can't seem find examples of how best utilize gem , set code.

seems reputation getting shot on here :(

here did. in creditcard model added next line:

validates_with validatecreditcard

i created file lib/validate_credit_card.rb follows:

class validatecreditcard < activemodel::validator def validate(record) if !creditcardvalidator::validator.valid?(record.card_number.to_s) record.errors[:base] << "the credit card number not valid." end end end

i had set next line config/application.rb

config.autoload_paths += %w( #{config.root}/lib )

this seems working, however, if best way of doing this?

also, though can see error message when come in invalid credit card number, credit card field doesn't surrounded in red, need whole errors[:base] , how works.

would appreciate feedback in regards beingness right way of doing type of validation.

many thanks.

here how got work.

i added next lines creditcard model:

validate :validate_credit_card private def validate_credit_card if !creditcardvalidator::validator.valid?(:card_number.to_s) errors.add(:card_number, "is not valid.") end end

ruby-on-rails credit-card

No comments:

Post a Comment