Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>According to the Rails Guides for i18n regarding <a href="http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models" rel="nofollow">Error Message Scopes (5.1.1)</a> for Active Record validation error messages, what you're attempting to do should work:</p> <blockquote> <p>Consider a User model with a validation for the name attribute like this:</p> <pre><code>class User &lt; ActiveRecord::Base validates :name, :presence =&gt; true end </code></pre> <p><em>&lt;...snip...></em></p> <p>When your models are additionally using inheritance then the messages are looked up in the inheritance chain.</p> <p>For example, you might have an Admin model inheriting from User:</p> <pre><code>class Admin &lt; User validates :name, :presence =&gt; true end </code></pre> <p>Then Active Record will look for messages in this order:</p> <pre><code>activerecord.errors.models.admin.attributes.name.blank activerecord.errors.models.admin.blank activerecord.errors.models.user.attributes.name.blank activerecord.errors.models.user.blank activerecord.errors.messages.blank errors.attributes.name.blank errors.messages.blank </code></pre> <p>This way you can provide special translations for various error messages at different points in your models inheritance chain and in the attributes, models, or default scopes.</p> </blockquote> <p>So, in your case, assuming your classes look something like this:</p> <p><strong>app/models/user.rb</strong></p> <pre><code>User &lt; ActiveRecord::Base validates :username, :format =&gt; {/regex/}, :message =&gt; :name_format end </code></pre> <p><strong>app/models/user_subclass.rb</strong></p> <pre><code>UserSubclass &lt; User validates :username, :format =&gt; {/regex/}, :message =&gt; :name_format end </code></pre> <p>and your <strong>config/locales/en.yml</strong> looks something like:</p> <pre><code>en: activerecord: errors: models: user: attributes: username: name_format: 'has the way-wrong format, bro!' </code></pre> <p>then the message searching for a validation on <code>UserSubClass</code> should go:</p> <pre><code>activerecord.errors.models.user_sublcass.attributes.username.name_format # fail activerecord.errors.models.user_sublcass.name_format # fail activerecord.errors.models.user.attributes.username.name_format # success activerecord.errors.models.user.name_format # ... </code></pre> <p>Assuming that your model files and yaml files look similar to what's above, then the potential complication you mentioned may be the issue, but obviously I can't be certain.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload