Rails 4 I18n: Using the Human Model Name in Association Approval Message

I have a simple object like:

class Question < ActiveRecord::Base
  belongs_to :company
  validates :company, presence: true
end

and I can’t get the verification message I18n to use the user model name for the error message.

I saved the key "activerecord.models.company" as "Firma" (German), and when I do Company.model_name.human, it returns "Firma", as expected. But the error message still displays as "Company" until I save "activerecord.attributes.question.company".

This is annoying because I need to add an attribute key for each model that checks connection with the Company (for example, "activerecord.attributes.user.company", "activerecord.attributes.project.company").

Is there a way to refer to the name of a human model instead of an attribute?

+4
source share
1 answer

I think that since in your form the company fields depend on the "has_many" relationship, through the fields_for helper, also using the "accepts_nested_attributes" in the model:

I suppose you have ...

in the shape of

<%= question.fields_for companies do |c| %>

... in the model

class Question<ActiveRecord::Base
  has_many :companies
  accepts_nested_attributes_for :companies,  :allow_destroy => :true

...

I think (so far) this is normal ... because Vopros companies can have one name, and Projects companies can have one more, etc.

+1
source

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


All Articles