Garrett Davis
Garrett Davis

Reputation: 339

Nested Models attribute localization for error messages in Rails 3.1

I have a deeply nested models form.

When a nested model attribute is in error the error messages is displaying:

List items identifier url may not be blank.

Which is:

Model_name + attribute + localization file error message

The correct message should be:

Item link url may not be blank.

ruby-1.9.2-p290 :014 > ListItem.human_attribute_name("identifier")
=> "Item Link" 

Localization is otherwise working fine except for nested model attribute names in error messages.

Looks like it was a bug in 2.3.4 that was fixed, but I can't figure it out.

Upvotes: 3

Views: 1483

Answers (2)

stefano salvucci
stefano salvucci

Reputation: 324

the following is working for me. I've a Course model and a OfflineCourse Model. OfflineCourse is nested in Course.

The following YAML works correctly form me. Hope this helps

  course:
    title: Titolo
    subtitle: Sottotitolo
    description: Descrizione
    abstract: Abstract
    audience: A chi è rivolto?
    topic: Argomenti
    typology: Tipologia
    stars: Stelle
  course/offline_courses:
    start_date: Data inizio
    end_date: Data fine
    location: Luogo
    schedule: Programma
    visible: Visibile
    city: Città

Upvotes: 5

SubmittedDenied
SubmittedDenied

Reputation: 957

Not sure if this the the "proper" way to solve this, but this will work.

In your localization file, you probably have something like this:

en:
  activerecord:
    attributes:
      list_item:
        identifier: Item Link

This will work as long as you address it from the nested model directly. Rails validations appears to go via the parent model, so you need something like:

en:
  activerecord:
    attributes:
      list_item:
        identifier: Item Link
      parent_model:
        list_item:
          identifier: Item Link

To me this seems to break the DRY principle because you have to repeat the human readable name at the nested level too, but this should work for you.

Upvotes: 0

Related Questions