I've got some form for user registration and want to localize it via locales yml files. Especially errors from validations.
For example, locale file:
tr:
activerecord:
errors:
models:
user:
attributes:
name:
blank: "can't be blank"
it will return: name can't be blank in errors area:
<% #user.errors.each do |error| -%>
<p><%= error %></p>
<% end -%>
Next step I want to create is to rename name attribute (and others) like that (this is what don't work):
tr:
attributes:
user:
name: "Real name"
to get this error after validation: Real name can't be blank
So where I should locale attribute names to translate them in error messages
Try this:
tr:
activerecord:
attributes:
modelname:
attributename: "translation"
Substituting modelname with the name of your model, and attributename with the name of the attribute you want to provide a translation for, here name.
Related
For the life of me, I cannot find how to remove "model name" before the error message. Typically this would be ok but hear me out.
I have a model called 'foo'. I'd need to rename it at some point but for now it's a hassle. For now, I need to change the error message: "Foo How often you get paid is missing".
# finance.rb
belongs_to :foo # this will be renamed in the future
[..]
I thought I needed to edit the en.yml only:
en:
activerecord:
errors:
models:
finance:
attributes:
foo:
required: "How often you get paid is missing"
This works but I don't need to show the model's name with the message. Ok I could do some string replace but that's ugly. Is it possible to only show the message in the en.yml?
EDIT:
Error are displayed as:
<% if #finance.errors.any? %>
<ul>
<% #finance.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
Seems to be an easy solution found here. So in my case:
en:
errors:
format: "%{message}"
activerecord:
errors:
models:
finance:
attributes:
repayment_type:
required: "How often you get paid is missing"
This could be a duplicate post so you could mark as duplicate 馃檪
I am trying to perform a simple customization on all of the validation error messages on a form in my rails app. Simply, I want to go from: can't be blank and is invalid to Can't be blank, Is invalid, etc. Is there a way to do this in my translations that won't require customizing every single individual error message?
If you have a class or some way of creating a css selector you can use:
.error-msg:first-letter{
text-transform: capitalize
}
Another option is you could change your config/locales/en.yml file like so:
en:
errors:
messages:
blank: "Can't be blank"
invalid: "Is invalid"
...
You could try changing it in your view using the capitalize method.
In your view:
<% m.errors.each do |attr, msg| %>
<%= msg.capitalize %>
<% end %>
I've got a set of locales, as listed below. When an error message is triggered, it will pull in the name of the attribute and prepends it before the error message.
The error result for the points value being blank on submit is, "value points value can't be blank".
How do I remove the {%attribute} name in the error message?
en:
activerecord:
attributes:
referral:
email: The email address you entered
errors:
models:
answer:
attributes:
value:
blank: points value can't be blank
I've also tried to add the message in the model, but to no avail (it still prepends the attribute name).
validates_presence_of :value, :message => "points value can't be blank"
Thanks in advance!
I would do this by localizing the attribute name, rather than prevent it from being added to the message:
en:
activerecord:
attributes:
answer:
value: "points"
You could try accessing each message value from the errors in the object you're trying to create, and within each message, to access it's first value (as it's an array), something like:
<% answer.errors.messages.values.each do |message| %>
<li><%= message.first %></li>
<% end %>
I've successfully created my he.yml to localize my model's attributes names,
example:
attributes:
vendor:
name: 砖诐 住驻拽
counter_number: 诪住驻专 讞砖讘讜谞讬转
phone: 讟诇驻讜谉
address: 讻转讜讘转
Now, displaying labels in forms using simple_form's f.input, displays it correctly, the translated value of each attribute.
the problem is, displaying errors after validation, using
<% #vendor.errors.each do |attribute, error| %>
|attribute| for error "counter_number" for example, is displayed: "counter_number".
not the translated one at the locale file [which as i mentioned previously, configured and loaded successfully].
I appended errors in a ul.errors, as shown in this screenshot:
Thanks in advance.
You can do something like this:
#vendor.errors.messages do |attribute, errors|
translated_attribute = Vendor.human_attribute_name(attribute)
errors = errors.join(", ")
end
I've tested this with my User model:
The following is just an example to complete your provided code.
<% #vendor.errors.each do |attribute, error| %>
<strong><%= t("activerecord.attributes.#{#vendor.class.to_s.underscore}.#{attribute}") %>:</strong>
<%= error.messages.to_sentence %>
<% end %>
Maybe there is an easier approach than this.
I want rails to show error message
Field <field name> can't be blank
but using standard means I get
<field name> Field <field name> can't be blank
Here's a minimal example reproducing the problem:
rails new test
cd test
rails g scaffold user name
rake db:migrate
Add validation to app/models/user.rb:
class User < ActiveRecord::Base
validates :name, presence: true
end
Edit config/locale/en.yml to be:
en:
activerecord:
attributes:
user:
name: "Name"
errors:
models:
user:
attributes:
name:
blank: "Field %{attribute} can't be blank"
After this start the server
rails s
point browser to http://localhost:3000/users/new and press "Create User" button. You'll get:
Apparently, there's another template somewhere, which says something like
%{attribute} %{message}
but I can't find it in rails code.
This is because in a standard view generated by scaffold (views/users/_form.html.erb) you have:
<% user.errors.full_messages.each do |message| %>
This is what returns
Name Field Name can't be blank
Instead, you can modify the _form view and use user.errors.messages, where you get a hash with errors assigned to keys representing fields:
#user.errors.messages
{:name=>["Field Name can't be blank"]}
To get what you expect you could write for example:
<% #user.errors.messages.values.flatten.each do |message| %>
<li><%= message %></li>
<% end %>