Rails 3 Change Error Message

Rails 3 Change Error Message

Yes it is actually really simple.

You should have a file named config/locales/en.yml, if not simply create one. There you can add your own custom names.

en:
activerecord:
models:
order: "Order"
attributes:
order:
b_name: "Business Name"

That will replace your b_name for "Business Name"

Your Order model in app/models/order.rb should look like:

class Order < ActiveRecord::Base
validates :b_name, :presence => true
.
.
.

Please let me know if it worked :)

Here is an screenshot of my app working fine.
Here is an screenshot of my app working

Change validation error message globally in Rails 3

Noah Miller provided the answer to my question in the comments..

It seems this question is a duplicate of:

Where are Default Validation Error Messages in Rails 3.0?

and

Changing default error messages in Rails

Customizing Devise error messages in Rails 3?

These validations are all defined in the validations module, and use the default Rails error messages.

You can override these in your model.

validates_format_of :email, :with=>email_regexp, :allow_blank => true, :message=>"new error message here" 

Customizing Model Validation error messages alerts

You can use :message option to assign custom error message.

Example:

validates :directions_from, presence: true, 
message: "'Direction from' really really really can't be blank!"

Then this custom error message will appear as <%= msg %> in the form view.

Ref: http://edgeguides.rubyonrails.org/active_record_validations.html#message

Add
To answer OP's question on the comment, i.e. the message shown in web page is not very friendly, showing result as "Directions directions from 'Direction from' really really really can't be blank"

The reason is the view template use errors.full_messages to show the error messages. You can easily customize it with two options:

Option 1: Write the custom message without subject. i.e. really can't be blank

Option 2: Write the message as before in full sentence, but refer to message only in view, instead of full_message

Example:

<% @hikingtrail.errors.messages.each do |msg| %>
<li><%= msg %></li>
<% end %>

Ref: http://rubydoc.info/docs/rails/3.2.8/ActiveModel/Errors (full_message is nothing more but a mix of attribute and message)

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 do you edit Devise error messages?

See the sources for the devise_error_messages! method at https://github.com/plataformatec/devise/blob/master/app/helpers/devise_helper.rb.

All the errors are inside

<div id="error_explanation"> 

so you can use that fact in your CSS. Inside it uses only basic styling: h2 for the header message, ul for the individual errors. See this SO example for #errorExplanation styling, for example: how to beautify validations in rails. Just don't forget to replace #errorExplanation with #error_explanation in the example.

But your best approach would probably still be to rewrite this method or write and use your own, and there apply all the styling you like.

I'd personally recommend displaying errors next to the fields they belong to. See this SO thread, for example, on how to do that: Rails: Errors close to particular fields in forms.

Another improvement would be switching to simple_form for your forms (and getting errors-next-to-fields for free). See, for example, an excellent Railscast on that: http://railscasts.com/episodes/234-simple-form. There's a more recent revised Railscast, but not sure if you're a Pro subscriber there.

Rails 3: how to generate custom error message from failed validation

Put a hash with the key message and desired message as the value instead of true:

validates :feed_id, presence: true, uniqueness: {message: "already subscribed"}


Related Topics



Leave a reply



Submit