3

I'm using Action Text for a Web CRUD I've created. The form has two main attributes:

title: Título

content: Contenido

Here's my form:

<%= simple_form_for(@announcement) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="form-inputs">

    <div class="form-group">
      <%= f.input :title, input_html: { class: 'form-control' }, label_html: { class: 'form-label' }, required: false %>
    </div>

    <div class="form-group">
      <label class="form-label"> <%= Announcement.human_attribute_name :cover %></label>
      <%= f.input :cover, label: false, input_html: { class: 'form-control' }, as: :file, label_html: { class: 'form-label' }, required: false %> 
    </div>

    <div class="form-group">
      <label class="form-label"><%= Announcement.human_attribute_name :content %></label>
      <%= f.rich_text_area :content, label_html: { class: 'form-label' }, required: false %>
    </div>

    <div class="form-group">
      <label class="form-label"> <%= Announcement.human_attribute_name :is_active %></label>
      <div class="custom-control custom-switch mr-2">
        <%= f.check_box :is_active, class: 'custom-control-input', id: 'is-active-check' %>
        <label class="custom-control-label" for="is-active-check" />
      </div>
    </div>

  </div> <!-- END FORM INPUTS -->

  <div class="form-actions">
    <%= f.button :button, class: 'btn btn-primary mt-3' %>              
  </div>

<% end %> <!-- END FORM -->

In my model, I'm validating the presence of the fields:

validates :title, :content, presence: true

Problem:

When I submit an empty form the title field shows the expected validation errors. However the content field (Action Text) does not. The empty content field is preventing the record from being saved (this is OK), but like I said is not showing the error in the form.

Please take the following image as reference:

enter image description here

Question:

How can I show the validation errors for the content field?

Luis de Haro
  • 711
  • 1
  • 7
  • 28
  • rich_text_area has been blatantly ignored by plataformatec folks (simple_form developers). Take a look at this question https://stackoverflow.com/questions/57455653/trix-editor-validation It might be helpful. – inmydelorean May 02 '20 at 23:31

1 Answers1

2

The simple_form gem added support for rich text areas in version 5.0.2.

With this version, simply write

<%= f.label :content, as: :rich_text_area %>

instead of

<%= f.rich_text_area :content %>

and simple_form will work its magic.

kateba7234
  • 21
  • 3