Rails 3 HOW TO remove attribute prefix from validation error messages

Traditional Chinese version is at RailsFun.

First, in most cases, you should translate displayed attribute name by i18n. Here are some solutions, including i18n way:
in Ruby Forum
in stackoverflow

But in my case, I just want to remove it from displaying. And if you want it too. This is my solution (thought a little bit ugly):

put it in initializers/override_human_attribute_name.rb
class ActiveRecord::Base
  def self.human_attribute_name(*attribute)
    return ""

And restart the rails server.

By the way, in in Ruby Forum. There is a solution that override human_attribute_name too with a custom mapping HUMANIZED_COLUMNS hash. But the solution doesn't work straightly. It's because in ActiveModel::Errors have it's own count method. Which need some calculate in human_attribute_name. And if you don't provide it in your override. It will fail on calling model_instance.errors.count

That's why i call super before returning empty string. Maybe you got better solution. Welcome provide in follow. I will appreciate your sharing!

No comments:

Post a Comment