form_for error messages in Ruby on Rails

前端 未结 4 865
离开以前
离开以前 2020-12-10 12:28

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

<%= form_for @post do |f| %>
  ...
<% end %&g         


        
4条回答
  •  半阙折子戏
    2020-12-10 13:01

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

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

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

提交回复
热议问题