Change Error Field Name in Rails

Change error field name in Rails

The general practice now-a-days is to edit your locals like so:

# config/locales/en.yml
en:
activerecord:
attributes:
user:
fname: "First Name"

Your error message will now say "First Name can't be..."

For completeness sake, you have another option. Which is to add the following to your User Model:

class User < ActiveRecord::Base

HUMANIZED_ATTRIBUTES = {
:fname => "First Name"
}

def self.human_attribute_name(attr, options = {}) # 'options' wasn't available in Rails 3, and prior versions.
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end

end

Changing Rails Active record column name 's error message

Change it like this:

class Contactu < ActiveRecord::Base
validate :cname, presence: { message: 'user-friendly message' }
validate :cdetails, presence: true
end

In order to hide field names, also use the following code to display your error messages:

model.errors.full_messages.join('<br>')

How to change change error messages on rails

I suppose you want to translate "Value must be grater than or equal to 0", if that's the case, what you need to do is create a translation for that on the locale file. In Spanish will be something like this:

# config/locales/es.yml
es:
activerecord:
errors:
models:
product:
attributes:
amount:
greater_than_or_equal_to: 'What ever you want to say'

Depending on your native language, you have to create the file and define the message, I think you are doing it already, because you are using translations:

#{t('product.shineer_irsen')}

You can find more information here:

http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

How to add custom validation message on field name without field name as a prefix?

You could write a custom validation, and add the error message to the record as a whole, instead of a particular attribute as follows:

validate :name_is_present

private

# Making this private is optional, but recommended
def name_is_present
errors.add(:base, "Promo code required") if name.blank?
end

For more details, refer to the explanation in Ruby guides here

How to change default validation error messages on belongs_to field

It has key required, try:

en:
activerecord:
errors:
messages:
required: "custom required message"


Related Topics



Leave a reply



Submit