Configuring model validation error notifications

I am trying to set up an error warning that users see at the top of the form when they enter data incorrectly. The error message I'm trying to configure relates to model attributes that are in a nested form.

I tried the solution here , which says that you need to edit the file config/locales/en.yml, but that only changes the message, not the model name and attribute that appear before the error message.

I also tried what Billy suggested in his answer , which has the same result. i.e.

1 error forbade saving this pedestrian train:
  - Direction directions from "My custom error message"

Is there a way to show a more convenient model name and attribute in my error message, or completely remove them from the error message?

Here is what I have:

config / locale / en.yml

    # Sample localization file for English. Add more files in this directory for other locales.
    # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en:  
  activerecord:
    models: 
      direction: "In the Getting There section"
    attributes:
      direction:
        directions_from: "From field"
    errors:
      full_messages:
      format: "%{message}"
      models:
        direction:
          attributes:
            directions_from:
              blank: "My Custom Blank Error Message"

Model

class Direction < ActiveRecord::Base
  belongs_to :hikingtrail

  attr_accessible :directions_by, :directions_desc, :directions_from

  validates :directions_from, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_by? || a.directions_desc? } }

  validates :directions_by, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_from? || a.directions_desc? } }

  validates :directions_desc, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_from? || a.directions_by? } }
end
+5
source share
1 answer

You can use the option :messageto assign a 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 %>a form.

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

OP , , -, , , " " " "

errors.full_messages . :

1: . .. really can't be blank

2: , message full_message

:

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

: http://rubydoc.info/docs/rails/3.2.8/ActiveModel/Errors (full_message - , attribute message)

+6

All Articles