使用 materialize 显示 simple_form 个错误

Use materialize to show simple_form errors

我想尽我所能利用 simple_form 上的物化错误。

我的表格是:

  <div class="input-field col s6 offset-s3">
    <%= f.email_field :email, required: true, autofocus: true, placeholder: "Email", class: "validate" %>
  </div>

我希望除了显示红色表格外,它还显示一条错误消息。

如果要显示错误,可以在初始化表单后添加。

<%= simple_form_for yourObject do |f| %>

<% if f.object.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize( f.object.errors.count, "error") %> prohibited this field_group from being saved:</h2>
    <ul>
    <% f.object.errors.full_messages.each do |message| %>
      <li><%= message %></li>
    <% end %>
    </ul>
  </div>
<% end %>

<% end %>

对于简单的表格你需要使用f.input,例如:

<%= f.input :email %>

表单提交到服务器后,这将为您提供输入和任何错误。

您需要在模型中进行验证才能实际生成表单将显示的错误。例如:

class User < ActiveRecord::Base
  validate :email, presence: true
end