Search code examples
simple-formruby-on-rails-6.1

Simple Form not showing errors at top


I have a simple_form form setup and it will show inline errors fine. I have had problems with some users not seeing these errors and have had requests for a clear enumeration at the top of the very long form. I've used the code setup from the Rails Tutorial:

 <% if object.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-error">
      The form contains <%= pluralize(object.errors.count, "error") %>.
    </div>
    <ul>
      <% object.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

Which is called as a partial (loaded from app/views/application) onto the form (object = f.object). It does not render, but I'll see the failure in the rails console in development. Any ideas why this won't show errors in this fashion? Is this some failure related to Turbo/Hotwire (there is a controller watching the text area to resize if content overflows)?

A much shorter form that also uses the same error partial (and exhibits the same behavior):

<%= simple_form_for [:admin, @annotation] do |f| %>
  <%= render 'error_messages', object: f.object %>

  <%= f.input :abbreviation,
              input_html: { minlength: 1 } %>
  <%= f.input :name,
              input_html: { minlength: 1 } %>
  <%= f.input :description,
              as: :text,
              input_html: { data: { controller: "textarea-autogrow"} },
              input_html: { minlength: 1 } %>
  <fieldset>
    <legend>Used For</legend>
    <%= f.input :oa,
                as: :boolean,
                label: "OA (Lodge, Chapter, Section) Issues" %>
    <%= f.input :council,
                as: :boolean,
                label: "Council (CSP, JSP, etc) Issues" %>
    <%= f.input :camp,
                as: :boolean,
                label: "Camp Issues" %>
  </fieldset>

  <%= f.button :submit,
               data: { disable_with: "Please wait..." }  %>
<% end %>

Solution

  • With some help from Michael Koper, we were able to sort this out. The controller methods were missing status: :unprocessable_entity on the format.html statements. So changing:

    format.html { render action: "new"}

    to

    format.html { render action: "new", status: :unprocessable_entity }

    Solved this issue.