Rails 3 HOW TO remove attribute prefix from validation error messages

Traditional Chinese version is at RailsFun.
正體中文版在台灣Rails正體中文論壇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)
    super(*attribute)
    return ""
  end
end

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!

留言

這個網誌中的熱門文章

神之雫 的 雫 (雨下) 怎麼唸、意思是什麼

[已解決] 入肉 (肏) 的注音/怎麼唸?意思是什麼?(兒童不宜)

哆啦A夢放上任何東西都無違和感、彷彿有音效的滑鼠墊 這裡買得到