【问题标题】:Rails 6 Action Text - Form Validation ErrorsRails 6 操作文本 - 表单验证错误
【发布时间】:2020-02-26 23:29:02
【问题描述】:

我正在为我创建的 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

问题:

当我提交一个空表单时,标题字段会显示预期的验证错误。但是内容字段(操作文本)没有。空的内容字段阻止记录被保存(这没关系),但就像我说的那样没有在表单中显示错误。

请参考以下图片:

问题:

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

【问题讨论】:

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


【解决方案1】:

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

有了这个版本,简单写

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

而不是

<%= f.rich_text_area :content %>

simple_form 会发挥它的魔力。

【讨论】:

    猜你喜欢
    • 2020-03-05
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多