Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form_for error messages in Ruby on Rails

What is the preferred way to display validation error messages using form_for in Rails 4?

<%= form_for @post do |f| %>
  ...
<% end %>
like image 767
Kyle Decot Avatar asked Jul 15 '13 19:07

Kyle Decot


1 Answers

This is how I am displaying them for my form object called @location:

<% if @location.errors.any? %>
<ul>
  <% @location.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
</ul>
<% end %>

Note: put the above code after the <%= form_for @location do |f| %> line

like image 88
Danny Avatar answered Nov 15 '22 19:11

Danny