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 %>
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 have a simple account_form.rb model that I use for users who register:
class AccountForm
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :company_name, :email, :password
validates_presence_of :company_name, :email, :password
end
Upon form submission I then use the values email, password, company_name to create other models like account, user, etc. I have this all in a transaction.
Now when there are validation errors in these other models, I am trying to merge them into the account_form model but I get an error when the model props are not the same:
NoMethodError (undefined method `name' for #<AccountForm:0x00007fbfb9bae5c0>):
For example I am merging like this when validation fails:
#account.errors.each { |err| #account_form.errors.add(*err) }
Is there a way to pass down these errors to the view even though they don't exist in the account_form model?
I am displaying the errors like:
<ul>
<% #account_form.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
The reason your code does not work as expected is because it expects #accounts.errors.each to pass attribute/message pairs to the given block. However, only the attribute name is being passed, not the message. In your case err is assigned the value :name rather than something like [:name, "is invalid"].
Iterate over the error entries instead:
#account.errors.entries.each { |err| #account_form.errors.add(*err) }
Aside: Consider the UX in this case. The user will likely be confused to see errors on fields which are not shown on the form.
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 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.