Validation Error Messages not Displaying – Ruby on Rails

Just another quick problem I ran into, the validation was indicating that an error had occurred however it did not show what the error was:

1 error prohibited this message from being saved:

— Usually an error would appear here —

Turns out the problem was pretty straight forward – it’s always the little things! I’d simply missed the each when displaying the error messages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%= form_for @location do |f| %>
  <% if @location.errors.any? %>
    <div id="error_explanation">
       <h2><%= pluralize(@location.errors.count, "error") %> prohibited this message from being saved:</h2>
       <ul>
         <% @location.errors.full_messages do |msg| %>
         <% #Should have been: @location.errors.full_messages.each do |msg| %>
         <li>
           <%= msg %>
         </li>
         <% end %>
       </ul>
    </div>
  <% end %>

Simply adding the correct code fixed the problem straight away.

1 error prohibited this message from being saved:

Country can’t be blank

Good Luck!

Leave a Reply