mercredi 19 août 2015

Updating the attributes in the rails errors

I am looking to update the model errors hash that the rails sent with humanized attributes name. For example for a model called Foo . I have an attribute called foo_nm . I want the translated to foo_name when I do @foo.errors assuming that @foo is an object of class Foo.

{"foo_nm"=>["can't be blank"]}

So far I tried to extend the human_attribute_name the following way

   HUMANIZED_ATTRIBUTES = {
    :foo_nm => "foo_name"
  }

  def self.human_attribute_name(attr, options={})
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super
  end

But this doesnt seem to modify the errors hash. Any ideas around on how I can update the errors hash would be appreciated

Aucun commentaire:

Enregistrer un commentaire