模态

时间:2016-07-05 10:34:55

标签: ruby-on-rails forms validation bootstrap-modal

我有一个包含办公室列表的网页。目前我正在尝试制作三种表格来编辑/添加/删除办公室。这就是我所拥有的:

模型

class ChangeOfficeAddress < ApplicationRecord
  belongs_to :office
  belongs_to :insurer
  belongs_to :city
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates_presence_of :email
  validates_format_of :email, with: VALID_EMAIL_REGEX
  validates_presence_of :edit_office_address
  validates_presence_of :add_office_address
  validates_presence_of :delete_office_address
  validates_presence_of :city_id
  validates_presence_of :insurer_id
  validates_presence_of :name
end

在视图中,我有部分模态:

<div id="addModal" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <div class="text-center">
          <div class="btn-group topbar header-buttons" role="group" aria-label="...">
            <%= link_to 'Add', '#', class: 'btn btn-default disabled' %>
            <%= link_to 'Edit', '#editModal', { 'class' => 'btn btn-default', 'data-toggle' => 'modal', 'data-dismiss' => 'modal' } %>
            <%= link_to 'Delete', '#deleteModal', { 'class' => 'btn btn-default', 'data-toggle' => 'modal', 'data-dismiss' => 'modal' } %>
          </div>
        </div>
      </div>
      <div class="modal-body">
        <%= form_for (@change_office_address), remote: true, format: :json, html: { class: :contact_form } do |f| %>
          <div id="error_explanation" style='display:none;' class="bg-danger text-danger alert fade in alert-danger alert-dismissable errors">
            <ul>
              <% if @change_office_address.errors.any? %>
                <% @change_office_address.errors.full_messages.each do |msg| %>
                  <li><%= msg %></li>
                <% end %>
              <% end %>
            </ul>
          </div>
          <%= f.text_field :name, placeholder: 'Name', class: 'form-control' %>

          <br>
          <%= f.text_field :email, placeholder: 'e-mail', class: 'form-control' %> <br>
          <%= f.label :city_id %>
          <%= f.collection_select :city_id, City.order(:name), :id, :name,
                                  { include_blank: true }, { class: 'form-control' } %>
          <br>
          <%= f.label :insurer_id, 'Insurer' %>
          <%= f.collection_select :insurer_id, Insurer.order(:short_name), :id, :short_name,
                                  { include_blank: true }, { class: 'form-control' } %>
          <br>
          <%= f.text_area :add_office_address, placeholder: 'Add address', class: 'form-control', cols: '30',
                          rows: '5' %> <br>
          <div class="text-center">
            <%= f.submit, class: 'btn btn-default' %>
          </div>
        <% end %>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>

另外两个模式,它们之间的唯一区别是:add_office_address:edit_office_address:delete_office_address取代。

如果模型中没有验证,则表单提交,一切正常,但当我向:add_office_address:edit_office_address:delete_office_address添加验证时,验证不会; t pass,因为这些字段(我的意思是:edit_office_address:delete_office_address:add_office_address)是空白的。

我如何制作不同的表格?谢谢你!

1 个答案:

答案 0 :(得分:1)

对于某些情况,[add|edit|delete] _ office_address的不同字段的原因是什么?

但是,要解决您的问题,您应该执行以下操作:

class ChangeOfficeAddress < ApplicationRecord
  belongs_to :office
  belongs_to :insurer
  belongs_to :city
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates_presence_of :email
  validates_format_of :email, with: VALID_EMAIL_REGEX
  validates_presence_of :edit_office_address, if: :edit_office_address_changed?
  validates_presence_of :add_office_address, if: :add_office_address_changed?
  validates_presence_of :delete_office_address, if: :delete_office_address_changed?
  validates_presence_of :city_id
  validates_presence_of :insurer_id
  validates_presence_of :name
end

如果有效,请告诉我。