Localize a nested virtual attribute in Rails

How can I localize a nested virtual attribute in Rails?

Model:

class User < ActiveRecord::Base
  attr_accessor :company_information # This is used in callbacks etc
end

and view:

= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: {class: 'form-horizontal'}) do |f|
  = devise_error_messages!
  = f.input :email
  = f.input :password
  = f.input :password_confirmation
  = f.simple_fields_for :company_information do |c|
    = c.input :name # This is what I want to localise
  = f.button :submit

Translation keys (from en.yml), such as activerecord.attributes.user.company_information.nameand activerecord.attributes.user.company_information_name, are not selected.

+3
source share
2 answers

It seems you are using simple_form gem to generate forms. Here is what worked for me.

en:
  simple_form:
    labels:
      user:
        company_information:
          name: My Name

A link to a simple localization chapter form can also be useful.

+1
source

In rails 3 I found one way. In your case, the translation will be helpers.label.user[company_information].namein en.yml, it will look like

en:
  helpers:
    label:
      "user[company_information]":
        name:"Name"
0

Source: https://habr.com/ru/post/1502955/


All Articles