Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 536099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:47:10+00:00 2026-05-13T09:47:10+00:00

Is there a generally accepted way of overriding error (validation) messages from a gem/plugin

  • 0

Is there a generally accepted way of overriding error (validation) messages from a gem/plugin in rails?

For example, I’m using the ActiveMerchant gem, and if someone puts in an American Express credit card number, but selects ‘MasterCard’, I get a not-very-descriptive “Type is not the correct card type” error.

I can easily get around this by doing something like this:

def validate_card
  unless credit_card.valid?
    credit_card.errors.full_messages.each do |message|
      if message =~ /is not the correct card type/i
        errors.add_to_base "This credit card number is invalid. 
                            Please ensure that you selected the correct type."
      else
        errors.add_to_base message
      end
    end
  end
end

but this technique quickly becomes unmaintainable and is clearly (at least in my opinion) far from ‘best-practices’.

Similarly, I could unpack the ActiveMerchant gem and hack it up to put in my own custom error messages, but that also seems unmaintainable as it would require the same hacks to added into future, unpacked, versions of ActiveMerchant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:47:11+00:00Added an answer on May 13, 2026 at 9:47 am

    In all honesty, you’re best bet is to either rewrite parts of the gem/plugin to suit your needs. Unfortunately if you decide to update the gem/plugin at any time you will lose your changes.

    However, Ruby is a dynamic language, so classes can be reopened, and you can override methods of any module/class from any file. Through the magic of open source, I’ve tracked down the module/class/method you’d need to meddle with to achieve your goal.

    Put the following in a file and ensure it gets loaded after ActiveMerchant (how to load it is dependent on whether you’re using the plugin or the gem)

    module ActiveMerchant
      module Billing
        class CreditCard
    
          private
          def validate_card_number #:nodoc:
            errors.add_to_base "This credit card number is invalid. \n" +
             "Please ensure that you selected the correct type." unless 
                CreditCard.valid_number?(number)
          end
        end
      end
    end
    

    N.B.: This method relies on ActiveMerchant internals, which is generally a bad idea. But I see it as the lesser of two evils to maintaing your own version of ActiveMerchant. Should you update the gem/plugin and something the above code relies on has changed, it could break in odd ways.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a succinct way, or a generally accepted way to convert from a
Is there any generally accepted, proven-to-work way using hibernate, that keeps a history of
Is there a generally accepted way of converting a Data.Sequence (Seq a) to a
Is there an easy (or generally accepted) way to load up a binary column
Is there a generally accepted way to comment functions in Python? Is the following
Are there any generally-accepted coding standards (naming, casting etc) that apply specifically to iPhone/Cocoa/Objective-C?
There are two ways to write a polymorphic migration in Rails. Generally, I've done
A generally accepted way to pass all data to a view is to have
Is there a way to write interface-based code (i.e. using interfaces rather than classes
Or are there other generally accepted patterns for creating a generic business application with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.