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

How to manage errors with simple_form in rails 6


I have a form where I have added some validation, I would like the errors to be listed, I have an idea that it can be done this way.

  = simple_form_for(@post) do |f|
    .row.pt-4
      .col-lg-3  
        -if f.object.errors[:base].present?
          .alert.alert-danger
            %ul 
              %li= f.error_notification message: f.object.errors[:base].to_sentence

The problem I have is that the first error is listing it correctly, but the others are not.

example: enter image description here

example with more errors enter image description here

it should be displayed every error with a bullet point


Solution

  • You need to loop over the errors:

    = simple_form_for(@post) do |f|
        .row.pt-4
          .col-lg-3  
            -if f.object.errors.any?
              .alert.alert-danger
                %ul
                  = f.object.errors.each do |error| 
                    %li= f.error_notification message: error.message