Skip to content

Instantly share code, notes, and snippets.

@skojin
Created October 9, 2009 12:25
Show Gist options
  • Save skojin/205971 to your computer and use it in GitHub Desktop.
Save skojin/205971 to your computer and use it in GitHub Desktop.
prefix validation message with ^ to display it without attribute name
# prefix validation message with ^ to display it without attribute name
# original idea: http://speakmy.name/2009/01/custom-validation-errors-in-rails-and-activerecord/
class ActiveRecord::Error
CUSTOM_PREFIX = '^'
def full_message_with_custom_prefix
if @message.is_a?(String) && @message.mb_chars[0..(CUSTOM_PREFIX.size-1)] == CUSTOM_PREFIX
@message.mb_chars[(CUSTOM_PREFIX.size)..-1]
else
full_message_without_custom_prefix
end
end
alias_method_chain :full_message, :custom_prefix
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment