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.
Related
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).
I am making a Rails app to replicate my currently-online arcade website that I wrote in PHP. The only controller I have right now is 'games' and I have some fields such as title and description. When I submit the form and I don't meet the requirements of the validators I have set in my models/game.rb, it will show a red bar underneath containing the error, such as " is too short (minimum is 20 characters)." For some reason, it doesn't put the field name in front of the word 'is'.
Yes, it is obvious when you look at it that it is referring to the field above, but there is definitely something wrong here. I have created other Rails apps in the past with the same issue and there actually hasn't been a time in the past few months that I've been playing with Rails that I've seen this working right.
Screenshot here: http://postimg.org/image/v3kt6xia1/
Below is my code.
models/game.rb
class Game < ActiveRecord::Base
$msg_success_icon = "fa fa-check fa-lg"
$game_categories = ["Adventure", "Arcade", "Action", "Racing", "Puzzle", "Strategy", "Sports", "Shooting", "Misc", "Movies"]
validates :title, {
:presence => true,
:length => {
:minimum => 2
}
}
validates :description, {
:presence => true,
:length => {
:minimum => 20
}
}
end
config/locales/en.yml
en:
activerecord:
models:
game:
one: Game
other: Games
attributes:
game:
title: 'Title'
description: 'Description'
game_width: 'Width'
game_height: 'Height'
category: ''
errors:
models:
game:
attributes:
title:
blank: 'okay fill that title field in!'
embed_url:
blank: 'must link to the game file (.swf & .dcr extensions allowed)'
thumb_url:
blank: 'must link to a image of the game (.jpg, .gif, & .png allowed)'
category:
inclusion: ''
messages:
too_short:
one: must be at least 1 character long.
other: must be at least %{count} characters long.
not_a_number: must be a number.
I compared this code to several examples that I found online and I can't figure out what I'm doing wrong. I feel like the problem lies elsewhere but I don't know where. Any help would be much appreciated.
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
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).
Suppose I have a model class like this:
class Shoebox < ActiveRecord::Base
validates_inclusion_of :description, :in => ["small", "medium"],
:message => I18n.t("activerecord.errors.models.shoebox.with_name",
:name => name)
end
And some yaml:
en:
activerecord:
errors:
models:
shoebox:
with_name: "the description of %{name} is not in the approved list"
And I create a new Shoebox:
s = Shoebox.new(:description => "large", :name => "Bob")
s.valid?
But when I look at the error (s.errors.first.message), I see:
"the description of Shoebox is not in
the approved list"
and not:
"the description of Bob is not in the
approved list"
I've tried :name => name, :name => :name, :name => lambda{name}, :name => lambda{:name}.
I've tried creating a helper method
def shoebox_name
name
end
And passing :name => shoebox_name, :name => :shoebox_name, :name => lambda{shoebox_name} and :name => lambda {:shoebox_name}.
How can I get the ivar value for name to be interpolated into the string?
Try removing the message option in the validation, and change your yaml to be:
en:
activerecord:
errors:
models:
shoebox:
description:
inclusion: "the description of %{name} is not in the approved list"
See http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models for more details
You can use a custom validation method to achieve what you are trying to do. All the columns are available in the custom validator:
validate :description_in
def description_in
if !(["small", "medium"].include?(description))
errors.add(:base, "The description of #{name} is not in the approved list")
end
end
PS: After a lot of googling around, I realized that it was much easier to implement a custom validator than search around.