25

in rails 3 i don't want to show field names in error messages. Anyone know how to do that ?

validates_presence_of :title, :message => "no title"

it shows

Title no title 

i want

no title
1
  • 2
    That's not duplicate. Commented Sep 2, 2016 at 8:31

6 Answers 6

34

In your form view change your current code

      <%@object.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>

With this

      <%@object.errors.messages.values.each do |msg| %>
        <%msg.each do  |m| %>
          <li><%= m %></li>
        <%end %>
      <% end %>
Sign up to request clarification or add additional context in comments.

1 Comment

If you don't want to use a double loop: <% @object.errors.messages.values.flatten.each do |error| %> <li><%= error %></li> <% end %>
24

This worked for us (Rails 4):

<% resource.errors.each do |attr,msg| %>
    <li><%= msg %></li>
<% end %>

1 Comment

Using Rails 4.0.2, this removed the error message text and left only the bullet.
3

This worked for me in Rails 4 (haml):

%ul
  - @some_object.errors.messages.each do |message|
    %li= message[1][0]

Comments

1
module ActiveModel
  class Errors
    def full_messages
      map { |attribute, message|
        message
      }
    end
  end
end

See also: Change displayed column name in rails

Comments

1

You can use the following Gem

https://github.com/jeremydurham/custom-err-msg

You can use a '^' character at the start of the message value. And it will only show the characters after this.

 validates_presence_of :title, :message => "^no title"

You can use the following Gem also

http://www.rubydoc.info/gems/dynamic_form/1.1.4

4 Comments

^ Does not work.
You have to use the gem. I am using it in my project.
dynamic form worked for me
0

If you change the label of the element it will effect the error message label. So if you change it to a blank string it will render just the message:

-# reviews/_form.html.haml
= form_for review do |form|
  = form.label :rating, (review.errors[:rating] ? "" : "Rate this Item" )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.