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

时间:2019-10-31 15:00:10

标签: simple-form ruby-on-rails-6 actiontext

我正在对创建的Web CRUD使用操作文本。表单具有两个主要属性:

标题:提图洛

内容:Contenido

这是我的表格:

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

问题:

当我提交空白表格时,标题字段将显示预期的验证错误。但是,内容字段(“操作文本”)没有。内容字段为空将阻止保存记录(这是可以的),但是就像我说的那样,它没有在表格中显示错误。

请参考下图:

enter image description here

问题:

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

1 个答案:

答案 0 :(得分:0)

simple_form gem 在 5.0.2 版本中添加了对富文本区域的支持。

有了这个版本,简单写

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

代替

<%= f.rich_text_area :content %>

并且 simple_form 会发挥它的魔力。