Skip to content

Commit 3295823

Browse files
Add ".[key]" to the abstract namespaces [ci skip] (rails#50860)
* Add ".[key]" to the abstract namespaces [ci skip] The section "Error Message Scopes" describes how the Rails look up the namespaces for a validation error message with code snippets. The first code snippet shows abstract namespaces like `errors.attributes.[attribute_name]` while the second one demonstrates the materialized one like `errors.attributes.name.blank`. The first code snippet lacks the validation error "key." My team member was confused and created a translation without "key", so I think it would be helpful to add ".[key]" to the abstract namespaces. * Move abstract key list after :blank examples Co-authored-by: Rafael Mendonça França <[email protected]>
1 parent 00ca9f5 commit 3295823

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

guides/source/i18n.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -915,17 +915,7 @@ class User < ApplicationRecord
915915
end
916916
```
917917

918-
The key for the error message in this case is `:blank`. Active Record will look up this key in the namespaces:
919-
920-
```
921-
activerecord.errors.models.[model_name].attributes.[attribute_name]
922-
activerecord.errors.models.[model_name]
923-
activerecord.errors.messages
924-
errors.attributes.[attribute_name]
925-
errors.messages
926-
```
927-
928-
Thus, in our example it will try the following keys in this order and return the first result:
918+
The key for the error message in this case is `:blank`. Thus, in our example it will try the following keys in this order and return the first result:
929919

930920
```
931921
activerecord.errors.models.user.attributes.name.blank
@@ -935,6 +925,17 @@ errors.attributes.name.blank
935925
errors.messages.blank
936926
```
937927

928+
To explain it more abstractly, it returns the first key that matches in the order of the following list.
929+
930+
```
931+
activerecord.errors.models.[model_name].attributes.[attribute_name].[key]
932+
activerecord.errors.models.[model_name].[key]
933+
activerecord.errors.messages.[key]
934+
errors.attributes.[attribute_name].[key]
935+
errors.messages.[key]
936+
```
937+
938+
938939
When your models are additionally using inheritance then the messages are looked up in the inheritance chain.
939940
940941
For example, you might have an Admin model inheriting from User:

0 commit comments

Comments
 (0)