0

Currently I have a script that checks to make sure that a test card was not submitted in production (since Authorize.net accepts test credit cards in live mode) and I add an error message to the credit card number if the one submitted is in the Test_Credit_Cards array like so:

TEST_CREDIT_CARDS = ['370000000000002', '6011000000000012', '4007000000027', '4012888818888']
validate :not_a_test_credit_card
 def not_a_test_credit_card
      self.errors[:cc_number] << "number is invalid!" if TEST_CREDIT_CARDS.include?(cc_number) and Rails.env.production?
    end

Now on the front end the error message appears like so: Credit card cc number is invalid!

Is there anyway to change the message to read Credit Card number is invalid! I just want to remove the cc portion from the message.

2
  • Are you sure it is not Credit card cc_number number is invalid ? Commented Oct 31, 2012 at 15:32
  • Yeah sorry, that was a typo on my part... I just want to be able to remove the attribute from my error message Commented Oct 31, 2012 at 15:34

2 Answers 2

1

First, remove the number from the error message:

  self.errors[:cc_number] << "is invalid!" if TEST_CREDIT_CARDS.include?(cc_number) and Rails.env.production?

Next, put a translation for cc_number in config/locales/en.yml:

en:
  activerecord:
    attributes:
      credit_card:
        cc_number: "number"

I prefer this answer over Anthony Alberto's answer for three reasons:

  1. It is always a good idea to use ActiveRecord existing translation engine.
  2. This way, cc_number will be translated to number whenever you add errors on it.
  3. It is not true that the error is on the CreditCard instance itself, it is specifically related to the cc_number field.
Sign up to request clarification or add additional context in comments.

Comments

0

That should do it :

self.errors[:base] << "number is invalid!" if TEST_CREDIT_CARDS.include?(cc_number) and Rails.env.production?

base refers to the object as a whole, not a specific attribute.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.