Ruby on rails Rails 5:验证表单的方法

Ruby on rails Rails 5:验证表单的方法,ruby-on-rails,Ruby On Rails,制作表格: <%= form_for(@contact, html: {multipart: true}) do |f| %> <div class="card"> <div class="card-header"> <h2 class="display-5 main-text-blue text-center font-weight-bold">Add New Contact</h2>

制作表格:

 <%= form_for(@contact, html: {multipart: true}) do |f| %>

      <div class="card">
        <div class="card-header">
          <h2 class="display-5 main-text-blue text-center font-weight-bold">Add New Contact</h2>
        </div>
        <div class="card-body">

     <div class="errors">
                <% if @contact.errors.any? %>
                    <div class="alert alert-danger">
                       <h5 class="text-center mb-n1 pb-1"><i class="fa fa-exclamation-triangle text-danger"></i> Please correct the following errors: </h5>
                   </div>
                <% end %>
            </div>

          <div class="form-group row">
            <div class="col-md-5 mx-auto">

            <div class="wrap" id="avatar-container">
                  <div class="valign-middle">
                      <div class="form-group">
                          <% if @contact.new_record? %>
                             <%= image_tag "100x100.png", class: "img-responsve img-preview" %>
                          <% else %>
                              <%= image_tag @contact.avatar, class: "img-responsve img-preview" %>
                             <% end %>
                          <label for="file" class="sr-only">Choose Image</label>
                           <%= f.file_field :contact_avatar, id: "file" %>
                      </div>
                  </div>
              </div>

            </div>
          </div>


          <div class="form-group row">
            <%= f.label :name, class: "col-lg-2 col-form-label" %> 
            <div class="col-lg-10">
              <%= f.text_field :name, class: "form-control #{'is-invalid' if has_error?(@contact, :name) }", id: "name", placeholder: "Name.." %>
                <% if has_error?(@contact, :name) %>
                      <span class="text-danger">
                          <%= get_error(@contact, :name)%>
                      </span>
                <% end %>
            </div>
          </div>


          <div class="form-group row">
             <%= f.label :email, class: "col-lg-2 col-form-label" %> 
            <div class="col-lg-10">
              <%= f.text_field :email, class: "form-control #{'is-invalid' if has_error?(@contact, :email) }", id: "email", placeholder: "Email.." %>
             <% if has_error?(@contact, :email) %>
                      <span class="text-danger">
                          <%= get_error(@contact, :email)%>
                      </span>
                <% end %>
            </div>
          </div>

          <div class="form-group row">
            <%= f.label :mobile, class: "col-lg-2 col-form-label" %> 
            <div class="col-lg-10">
              <%= f.text_field :mobile, class: "form-control #{'is-invalid' if has_error?(@contact, :mobile) }", id: "mobile", placeholder: "Mobile.." %>
              <% if has_error?(@contact, :mobile) %>
                      <span class="text-danger">
                          <%= get_error(@contact, :mobile)%>
                      </span>
                <% end %>
            </div>
          </div>


          <div class="form-group row">
             <%= f.label :phone, class: "col-lg-2 col-form-label" %> 
            <div class="col-lg-10">
             <%= f.text_field :phone, class: "form-control #{'is-invalid' if has_error?(@contact, :phone) }", id: "phone", placeholder: "Phone.." %>
              <% if has_error?(@contact, :phone) %>
                      <span class="text-danger">
                          <%= get_error(@contact, :phone)%>
                      </span>
                <% end %>
            </div>
          </div>


          <div class="form-group row">
            <%= f.label :country, class: "col-lg-2 col-form-label" %>
            <div class="col-lg-10">
               <%= f.text_field :country, class: "form-control", id: "country", placeholder: "Country.." %>
            </div>
          </div>


          <div class="form-group row">
            <%= f.label :address, class: "col-lg-2 col-form-label" %>
            <div class="col-lg-10">
              <%= f.text_field :address, class: "form-control", id: "address", placeholder: "Address.." %>
            </div>
          </div>
          <div class="form-row">
             <%= f.label :location, class: "col-lg-2 col-form-label" %>
            <div class="col-4">
             <%= f.text_field :city, class: "form-control", id: "city", placeholder: "City.." %>
            </div>
            <div class="col-4">
              <%= f.text_field :state, class: "form-control", id: "state", placeholder: "State.." %>
            </div>
            <div class="col-2">
               <%= f.text_field :zip, class: "form-control", id: "zip", placeholder: "Zip.." %>
            </div>
          </div>
          <div class="form-group row category-mt">
             <%= f.label :category, class: "col-lg-2 col-form-label" %>
            <div class="col-lg-5">
                <%= f.collection_select :category_id, Category.all, :id, :name, { prompt: "Select Category" }, id: "category_select", class: "form-control #{'is-invalid' if has_error?(@contact, :category) }" %>
                 <% if has_error?(@contact, :category) %>
                      <span class="text-danger">
                          <%= get_error(@contact, :category)%>
                      </span>
                <% end %>
            </div>
            <div class="col-lg-3">
              <a class="btn btn-outline-secondary add-category-button btn-block mt-1" href="#" id="add-category-btn">Add Category</a>
            </div>
          </div>

          <div class="form-group row" id="add-new-category">
            <label class="col-lg-2 col-form-label" for="location">New Category:</label>
            <div class="col-lg-10">
              <div class="input-group">
                    <input id="new-category" name="new-category" class="form-control <%= 'is-invalid' if has_error?(@category, :name)  %>"  placeholder="Enter category name" type="text">
                     <% if has_error?(@category, :name) %>
                      <span class="text-danger">
                          <%= get_error(@category, :name)%>
                      </span>
                    <% end %>


                <div class="input-group-append">
                    <button class="btn btn-outline-secondary category-btn" id="save-new-category-btn" type="button"><i class="fa fa-check"></i></button>
                </div>
              </div>
            </div>
          </div>

          <div class="form-group row">
              <%= f.label :note, class: "col-lg-2 col-form-label" %>
            <div class="col-lg-10">
               <%= f.text_area :note, class: "form-control", id: "note", placeholder: "Note..", rows: "3" %>
            </div>
          </div>
        </div>
        <div class="card-footer">
           <%= f.submit "Save", class: "btn btn-primary border-button mb-3 ml-3", id: "save-btn" %>
           <a class="btn btn-outline-secondary border-button mt-n3" data-dismiss="modal" href="#" id="cancel-btn">Cancel</a>
        </div>
      </div>

       <% end %>
如果出现错误,只需在底部打印错误文本。以下是该操作的帮助程序代码:

def has_error?(resource, field)
     resource.errors.messages[field].present?
  end

  def get_error(resource, field)
    msg = resource.errors.messages[field]
    field.to_s.capitalize + " " + msg.join(' and ') + '.'
  end
所以,基本上我在处理两个表:联系人和类别。到目前为止,我在表格上核实的大部分是联系人。我有这个特性,用户可以通过ajax即时添加新的类别,ajax是contacts表上的外键,也可以在上面的代码中看到

现在,据我所知,我还可以添加模型验证。因此,我尝试对我的类别进行验证:

 validates :name, uniqueness: true
我希望这将验证输出的新类别的唯一性,这意味着如果数据库中已经存在文本(类别名称),则必须禁止该文本并抛出错误。因此,我尝试了以下代码来显示错误:

<input id="new-category" name="new-category" class="form-control <%= 'is-invalid' if has_error?(@category, :name)  %>"  placeholder="Enter category name" type="text">
<% if has_error?(@category, :name) %>
<span class="text-danger">
 <%= get_error(@category, :name)%>
</span>
<% end %>

所以我认为它会工作,但它只是抛出一个错误,说 nil的
未定义方法错误:NilClass
,我认为它应该可以工作,因为我正在使用
@category
,但后来我意识到它是
@contact
的一种形式


是否有更好的方法来实现此操作并在“添加新类别”字段中显示错误?我真的被这件事缠住了。我希望有人能帮助我

您可以通过
f
对象获取类别错误

<% f.object.categories.each do |category| %>
  <input id="new-category" name="new-category" class="form-control <%= 'is-invalid' if has_error?(category, :name)  %>"  placeholder="Enter category name" type="text">
  <% if has_error?(category, :name) %>
    <span class="text-danger">
      <%= get_error(category, :name)%>
    </span>
  <% end %>
<% end %>


未定义的方法“类别”用于#您是否有关联
联系人
模型上有多个:类别
???
<input id="new-category" name="new-category" class="form-control <%= 'is-invalid' if has_error?(@category, :name)  %>"  placeholder="Enter category name" type="text">
<% if has_error?(@category, :name) %>
<span class="text-danger">
 <%= get_error(@category, :name)%>
</span>
<% end %>
<% f.object.categories.each do |category| %>
  <input id="new-category" name="new-category" class="form-control <%= 'is-invalid' if has_error?(category, :name)  %>"  placeholder="Enter category name" type="text">
  <% if has_error?(category, :name) %>
    <span class="text-danger">
      <%= get_error(category, :name)%>
    </span>
  <% end %>
<% end %>