errors.full_messages: attribute name appears twice - ruby-on-rails

This has been bugging me for a while. This problem occurs with all of my models, but i'll use one of them, Quiz, as an example.
Quiz has the following validations:
validates_presence_of :size, :style
I'm using I18n, and i have the following set in my translations file: (these are just the standard error messages, but i've included them in my en.yml so that it's easy to see the structure, if i want to override them for any particular model)
activerecord:
errors:
messages:
inclusion: "{{attribute}} is not included in the list"
invalid: "{{attribute}} is invalid"
empty: "{{attribute}} can't be empty"
blank: "{{attribute}} can't be blank"
record_invalid: "Validation failed: {{errors}}"
The problem is this: if i make a new quiz, which will fail validation, then look at quiz.errors.full_messages, each error message has the attribute then the full message:
>> quiz = Quiz.create
=> <unsaved quiz object>
>> quiz.errors.full_messages
=> ["Size Size can't be blank", "Style Style can't be blank"]
I don't understand why the message is, for example, "Size Size can't be blank" and not "Size can't be blank"
Any ideas anyone?

There should be also:
en:
errors:
# The default format to use in full error messages.
format: "%{attribute} %{message}"
And your other translations shouldn't include %{attribute} anymore.
To make sure you get all correctly use en.yml from your Rails version,
it is located at: lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/locale/en.yml

I just figured this out and thought i'd answer it myself in case anyone else had this problem: i had to amend the activerecord part of my translations file thus:
activerecord:
errors:
full_messages:
format: "{{message}}"
#define standard error messages, which we can overide on per model/per attribute basis further down
messages:
inclusion: "{{attribute}} is not included in the list"
exclusion: "{{attribute}} is reserved"
The problem was that the activerecord.errors.full_messages.format key was set (in vendor/rails/activerecord/lib/active_record/locale/en.yml) to "{{attribute}} {{message}}", and the messages in turn were set to "{{attribute}} can't be blank" for example. So the full_message was coming out as "{{attribute}} {{attribute}} can't be blank". Changing it to be just "{{message}}" fixed this.

Related

ActiveRecord Validations : String added to error message in locales

I'm trying customize validation error messages using i18n.
Let's say I have an Address model that validates the presence of a zip_code.
My config/locales/activerecord.fr.yml looks like this
fr:
activerecord:
errors:
models:
address:
attributes:
civility:
blank: "Some message"
The issue if I fail the validation, the error message I'l have in #address.errors.full_messages will be:
"Zip code Some message"
Why does Zip code gets added to my error message ? And how can I avoid this behaviour ?
You can just add:
errors:
format: "%{message}"
This way you will just show error message with out attribute name.
Edit: this should be added to config/locales/fr.yml and not config/locales/activerecord.fr.yml (which also has errors:)
Found a gem which will solve your purpose
With the help of this gem, You just need to start the locale message with a caret and it shouldn't display the attribute name in the message.
A model defined as:
class Item < ApplicationRecord
validates :name, presence: true
end
with the following en.yml:
en:
activerecord:
errors:
models:
item:
attributes:
name:
blank: "^You can't create an item without a name."
item.errors.full_messages will display:
#You can't create an item without a name
instead of the usual
#Name You can't create an item without a name
You could simply use:
#address.errors.messages[:zip_code] # or #address.errors[:zip_code]
#=> ['Some message']

Can I specify a default error message format for a single validator across all attributes?

I know you can do this:
# config/locales/en.yml
en:
activerecord:
attributes:
user:
email: "E-mail address"
errors:
models:
user:
attributes:
email:
blank: "is required"
via https://stackoverflow.com/a/2859275/718050
Question 1
Is it possible to specify the message for blank across an entire model, or even sitewide, instead of going into every single attribute?
Question 2
Also, it seems that blank comes from :presence in the model, e.g.
validates :email, :presence => true
So if a :presence => true error translates to blank:, where can I find a list of these translations? How am I supposed to know what :unique => true turns into inside en.yml?
This list is here and here
as you can see you can redefine blank error like this:
en:
errors:
messages:
blank: "can't be blank"

Trouble on using the i18n gem with "resources of resources"

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, ...
... in my config/routes.rb file I have:
resources :users do
resource :account
end
... in my app/models/users/account.rb file I have:
class Users::Account < ActiveRecord::Base
validates :firstname,
:presence => true
...
end
... in my config/locales/models/user/account/en.yml file I have:
en:
activerecord:
errors:
messages:
presence: "custom presence message - english"
... in my config/locales/models/user/account/it.yml file I have:
it:
activerecord:
errors:
messages:
presence: "custom presence message - italian"
The above code doesn't display in front end the "custom presence message" (it still displays the default RoR presence message: can not be blank). Furthermore if in my app/models/users/account.rb file I use:
class Users::Account < ActiveRecord::Base
validates :firstname,
:presence => { :message => t(:presence) } # Here I try to use the i18n helper method
...
end
I get the following error:
NoMethodError (undefined method `t' for #<Class:0x000001075bbc80>)
Why I get the NoMethodError?
Is the problem related to how I am organizing in directories my locale files? At this time (as stated in the official RoR guide) my file system is:
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
In few words, I would like to display my "custom presence message" only on validating "resources of resources" kind of Users::Account. How can I do that?
I also tried to state the following code in the config/locales/models/user/account/en.yml file
en:
activerecord:
errors:
models:
user:
account:
attributes:
firstname:
blank: "custom presence message - english"
but it doesn't work. Anyway the following works but I need different translations for different attributes (as I tried to state in the previous code example):
en:
activerecord:
errors:
messages:
blank: "custom presence message - english"
ok, due to this: https://github.com/rails/rails/issues/1402 last comment nested model look-up is removed
so try maybe something like
activerecord:
errors:
models:
users:
account:
attributes:
first_name:
blank: "You should fill up first name field to complete that"
and change inside :message hash to
I18n.t(:"activerecord.errors.models.users.account.attributes.first_name.blank")
and try avoid nested models ;-)
update:
after some debuging this will work:
activerecord:
errors:
models:
"users/account":
attributes:
first_name:
blank: "You should fill up first name field to complete that"

Rails 3 Customer Error message

I'm trying to do something very simple. I have a validates presence of in rails 3:
validates_presence_of [:first_nm]
When this fires, it gives the following crappy message:
"first nm cannot be blank"
I want to override the error message to give a friendly field name for "first nm"
"Please fill out your First Name"
I have seen all kinds of plugins, localization, humanized attributes tutorials, but these don't seem to work or are outdated. Is there no simple way to do this in Rails 3?
For localization, i've tried this:
# Sample localization file for English. Add more files in this directory for other locales.
# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en:
activerecord:
errors:
messages:
taken: "has already been taken"
record_invalid: "Validation failed: %{errors}"
models:
customer:
blank: "This is a custom blank message for %{model}: %{attribute}"
attributes:
first_nm:
blank: "This is a custom blank message for first name"
Alas, no luck. My error message did not change.
On thing that might be related. I'm not inheriting from ActiveRecord, because this object is getting saved via soap, not database. Instead, I have the following:
class Customer
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
end
Have you seen this page on ActiveRecord localization? It seems to be for Rails 3. I can't test it right now, but by the document it seems you can do:
models.user.attributes.first_nm.blank = "Please fill out your First Name"

Ruby on Rails: controlling the layout of an error message in ActiveRecord

My error messages in Rails look like the following:
"Email Your email is invalid."
Why is the name of the field prefixed within the string itself? It makes the error messages look awfully odd.
Is there anyway to circumvent this behavior so that I can just see "Your email is invalid."
The documentation for generate_full_message might be of use:
The default full_message format for any locale is "{{attribute}} {{message}}". One can specify locale specific default full_message format by storing it as a translation for the key: "activerecord.errors.full_messages.format".
Additionally one can specify a validation specific error message format by storing a translation for: "activerecord.errors.full_messages.[message_key]". E.g. the full_message format for any validation that uses :blank as a message key (such as validates_presence_of) can be stored to: "activerecord.errors.full_messages.blank".
Because the message key used by a validation can be overwritten on the validates_* class macro level one can customize the full_message format for any particular validation:
# app/models/article.rb
class Article < ActiveRecord::Base
validates_presence_of :title, :message => :"title.blank"
end
# config/locales/en.yml
en:
activerecord:
errors:
full_messages:
title:
blank: This title is screwed!
In your case, since you're using the default {{attribute}} {{message}} format, you're getting "Email" for the attribute and "Your email is invalid" as the message. You could change the :message argument to "is not a valid format" and you should get "Email is not a valid format". That's the quick fix. If you want to fully customize it you can use the locale method above.

Resources