Rails 6 动作文本 - 表单验证错误

问题描述 投票:3回答:1

我正在为我创建的一个Web CRUD使用Action Text。该表单有两个主要属性。

title: Título

内容。内容:

这是我的表格。

<%= 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 -->

在我的模型中,我在验证字段的存在。

validates :title, :content, presence: true

问题:

当我提交一个空表格时,标题字段显示了预期的验证错误。然而内容字段(Action Text)却没有。空内容字段阻止了记录的保存(这是确定的),但就像我说的那样没有在表单中显示错误。

请参考下面的图片。

enter image description here

问题:

如何显示内容字段的验证错误?

simple-form ruby-on-rails-6 actiontext
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.