Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localise nested virtual attribute in Rails

How is it possible to localise nested virtual attribute in Rails?

The model:

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

and the 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

The translation keys (from en.yml) like activerecord.attributes.user.company_information.name and activerecord.attributes.user.company_information_name aren't picked up.

like image 707
Dmytrii Nagirniak Avatar asked Mar 01 '26 11:03

Dmytrii Nagirniak


1 Answers

It seems you are using simple_form gem for generation of forms. Here is what worked for me.

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

The link to simple form's localization chapter may be also useful.

like image 57
luckyjazzbo Avatar answered Mar 03 '26 00:03

luckyjazzbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!