How to rename table in validates - ruby-on-rails

I'm doing validations like this: validates :name_user, presence: true
But I would like to change the name of the field "name_user" in the message: "column name_user can not be blank" appears "column first name can not be blank"

set up a translation for "name_user" in your locales. You can change both the name of the field and what the exact wording of the error message should be.
# config/locales/en.yml
en:
activerecord:
attributes:
user:
name_user: "First name"
errors:
models:
user:
attributes:
name_user:
blank: "is required"
You can extend it for additional fields and even other models...
# config/locales/en.yml
en:
activerecord:
attributes:
user:
name_user: "First name"
surname_user: "Last name"
department:
created_year: "Year established"
errors:
models:
user:
attributes:
name_user:
blank: "is required"

You can use the message option for this, as described here in the Rails guide.
validates :name_user, presence: { message: "First name cannot be blank" }
Now all your errors will use that strange. You can even make reference to the value that was passed in (as described in the Rails guide).

Related

Formatting model.errors.full_messages [duplicate]

This question already has answers here:
Fully custom validation error message with Rails
(18 answers)
Closed 5 years ago.
How do you format columns names in error messages?
class Person
validates_presence_of :name, :address, :email
validates_length_of :name, in: 5..30
end
person = Person.create(address: '123 First St.')
person.errors.full_messages
# => ["Name is too short (minimum is 5 characters)",
"Name can't be blank", "Email can't be blank"]
For example, when there is an error instead I want it to print
Full name can't be blank. (instead of 'Name')
How do I do this since in the model/database its stored as :name.
Is there someway I can link a string to :name?
You can specify a custom message.
validates_length_of :name, in: 5..30, message: 'Full name must be between 5 and 30 characters'
http://guides.rubyonrails.org/active_record_validations.html#message
You can also just translate the attribute
en:
activerecord:
attributes:
person:
name: "Full name"
By adding that to the config/locales/en.yml file
http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

In RoR, how do I customize a validation error message that is set to numericality greater than zero?

I’m using Rails 4.2.7. I have this validation rule for my model
class MyObjectTime < ActiveRecord::Base
validates :time_in_ms, numericality: { greater_than: 0 }
I want to write a custom error message, so in my config/locales/en.yml file I included
en:
activerecord:
attributes:
my_object:
name: "Name"
my_object_time:
time_in_ms: "Time"
errors:
models:
my_object_time:
attributes:
time_in_ms:
not_a_number: "This field must be a number greater than zero."
blank: "This field must be a number greater than zero."
But when my object fails to validate, this custom error message is not included in my #my_object_time.errors.full_messages array . Instead what is included is
Must be greater than 0
What is the proper rule to write in my config file so that I can customize this error message?
You can customize message directly in model.
class MyObjectTime < ActiveRecord::Base
validates :time_in_ms, numericality: { greater_than: 0 }, message: 'my customized message.'
end
or in your en.yml file you need to add message for greater_than:
en:
activerecord:
attributes:
my_object:
name: "Name"
my_object_time:
time_in_ms: "Time"
errors:
models:
my_object_time:
attributes:
time_in_ms:
not_a_number: "This field must be a number greater than zero."
blank: "This field must be a number greater than zero."
greater_than: "my custom message for greater than"

How can I translate the ActiveRecord attribute name in Rails 3.2?

I have the following validation in my model ( User ):
validates :first_name,:length => {:minimum => 2,:maximum => 50},:format => { :with => /[a-zA-Z]+/ }
I have the following in my yml locales file:
attributes:
first_name:
too_short: "First name is too short"
too_long: "First name is too long"
invalid: "First name is not valid"
Now, if I start a rails console, and write the following:
a = User.new
a.valid?
a.errors.full_messages
I will see the following errors:
["First name First name is too short", "First name First name is not valid"]
As you can see, the attribute name is also prepended to the field error. So far, everywhere in my code, I have used model.errors[:field], and this will always show me the string I have in the yml file, but I'd like to change the strings to:
attributes:
first_name:
too_short: " is too short"
too_long: " is too long"
invalid: " is not valid"
And use the full_messages version. The problem is, I don't know how to translate the attribute name. Let's say for example, that I'd like instead of First name, to have Name first. How would I do it?
You can find the answer here http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
in your config/locale/(your_lang).yml
en:
activerecord:
models:
user: Dude
attributes:
user:
first_name: "Name first"
change "en:" with the language symbol that you need to use
cheers
In Rails 5, after attributes, I had to namespace with my model's name in underscore. Like this:
pt-BR:
activerecord:
attributes:
my_model_name_in_underscore:
attribute_name: 'String'
Source: https://stackoverflow.com/a/5526812/1290457

Rails Validations and custom error messages

I'm having an issue with Validations, if I use the following syntax all is well (no failures).
validates :title, uniqueness: true
However if I change it to this, I get a failure.
validates :title, uniqueness: {message: 'Title must be unique'}
Here is the test for completeness:
test "product is not valid without a unique title " do
product = Product.new( title: products(:ruby).title,
description: "yyy",
price: 1,
image_url: "fred.gif" )
assert !product.save
assert_equal "has already been taken", product.errors['title'].join('; ')
end
I have a fixture that adds the book title for the Ruby product etc.
As I understand it the two validations should be the same, just that one gives a custom error message. This is the error I get when using the custom message.
1) Failure:
test_product_is_not_valid_without_a_unique_title_(ProductTest)
<"has already been taken"> expected but was
<"Title must be unique">.
Thanks in advance for your help.
Here:
assert_equal "has already been taken", product.errors['title'].join('; ')
you expect to see "has already been taken" in the errors hash, but you've overridden this message with your custom message. Test shows that your custom message appears as it should. Why do you still expect the default message? You have to expect Title must be unique.
TIP Don't supply the name of your field in your custom message. Rails will handle it automatically when generating errors by, say, product.errors.full_messages ("must be unique" would be enough).

Rails doesn't use human name for error message

in my translation file
activerecord:
models:
subject_choice: "Subject Choice"
subject_preference: "Subject Preference"
art_subject_choice: "Group 1 Preference"
science_subject_choice: "Group 2 Preference"
attributes:
student:
in_class: "Class"
subject_prefernce:
math_preference_type:
m1: "M1"
m2: "M2"
m1_m2: "M1>M2"
m2_m1: "M2>M1"
subject:
subject_type:
science: "Science"
art: "Art"
elective: "Elective"
the validation is done in subject_preference model. but the error show on page is "Subject preference base Art priority cannot be same as science priority."
How can I make it display model name correctly?
UPDATE:
I just want to get rid of "Subject preference base", how can i do it? Thanks
errors[:base] << "Duplicated priority in science subject"
I tried a different approach, for example
en:
activerecord:
models:
message:
attributes:
message:
content: ""
subject: ""
and in my model:
validates :subject, :presence => { :message => I18n.t('validation.category')}
This approach ensures only the validation message itself is shown.

Resources