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

Ask A Question

Stats

  • Questions 260k
  • Answers 260k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I suggest you read the spec. In short (this isn't… May 13, 2026 at 11:22 am
  • Editorial Team
    Editorial Team added an answer According to the SWFObject API, you could do something like… May 13, 2026 at 11:22 am
  • Editorial Team
    Editorial Team added an answer Are you using ActiveRecord? If so, you can use the… May 13, 2026 at 11:22 am

Related Questions

According to MSDN , a hash function must have the following properties: If two
I am maintaining a SOAP web service (ASP.NET version 2.0) and I have to
I have a web application written in C# that consumes several internal web services.
When I learned about proxy_options I started using it to test all my named
I know that this is somewhat subjective, but I wonder if there is a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.