Remove attribute name from locales error message - ruby-on-rails

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 %>

Related

Hide/remove model name in association error message

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 🙂

How to capitalize all Rails Activerecord form validation messages

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 %>

Where do I set template for rails error messages?

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 %>

How can I add a red error message for wrong input in the form in rail3?

So in rails, if any of the input is not valid, the page returns back to the input page and red errors will show up next to that input_text or textarea.
For example,
<%
validates_presence_of :email
%>
It will say the error at the top, and the email input text turns into red.
However, when I separately made a valid checking statement in a model, for example,
<%
def valid_email?
TMail::Address.parse(self.email)
rescue
errors.add("Please type in the correct email address.")
end
%>
It will say the error at the top, but the input_text itself does not turn into red.
How can I manually set the input text to be turn into red??
It should be:
self.errors.add(:email, "Please type in the correct email address.")
try this...
self.errors.messages[:email] = ["your error message"]

using i18n with rails 3 problem

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.

Resources