Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble on using the i18n gem with "resources of resources"
    primarykey
    data
    text
    <p>I am using Ruby on Rails 3.1 and I would like to know how to correctly handle internationalization related to "resources of resources". That is, ...</p> <p>... in my <code>config/routes.rb</code> file I have:</p> <pre><code>resources :users do resource :account end </code></pre> <p>... in my <code>app/models/users/account.rb</code> file I have:</p> <pre><code>class Users::Account &lt; ActiveRecord::Base validates :firstname, :presence =&gt; true ... end </code></pre> <p>... in my <code>config/locales/models/user/account/en.yml</code> file I have:</p> <pre><code>en: activerecord: errors: messages: presence: "custom presence message - english" </code></pre> <p>... in my <code>config/locales/models/user/account/it.yml</code> file I have:</p> <pre><code>it: activerecord: errors: messages: presence: "custom presence message - italian" </code></pre> <p><em>The above code doesn't display in front end the "<code>custom presence message</code>"</em> (it still displays the default RoR presence message: <code>can not be blank</code>). Furthermore if in my <code>app/models/users/account.rb</code> file I use:</p> <pre><code>class Users::Account &lt; ActiveRecord::Base validates :firstname, :presence =&gt; { :message =&gt; t(:presence) } # Here I try to use the i18n helper method ... end </code></pre> <p>I get the following error:</p> <pre><code>NoMethodError (undefined method `t' for #&lt;Class:0x000001075bbc80&gt;) </code></pre> <p><strong>Why I get the <code>NoMethodError</code>?</strong></p> <p><strong>Is the problem related to how I am organizing in directories my locale files?</strong> At this time (as stated in the <a href="http://guides.rubyonrails.org/i18n.html#organization-of-locale-files" rel="nofollow">official RoR guide</a>) my file system is:</p> <pre><code>config/locales/defaults/en.yml config/locales/defaults/it.yml config/locales/models/user/en.yml config/locales/models/user/it.yml config/locales/models/user/account/en.yml config/locales/models/user/account/it.yml </code></pre> <p><em>In few words, I would like to display my "<code>custom presence message</code>" only on validating "resources of resources" kind of <code>Users::Account</code>.</em> <strong>How can I do that?</strong></p> <hr> <p>I also tried to state the following code in the <code>config/locales/models/user/account/en.yml</code> file</p> <pre><code>en: activerecord: errors: models: user: account: attributes: firstname: blank: "custom presence message - english" </code></pre> <p>but it doesn't work. Anyway the following works but <em>I need different translations for different attributes</em> (as I tried to state in the previous code example):</p> <pre><code>en: activerecord: errors: messages: blank: "custom presence message - english" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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