在Rails中提交bootstrap模态窗体后,如何显示有错误的模态?

时间:2016-07-13 20:55:00

标签: javascript ruby-on-rails twitter-bootstrap bootstrap-modal simple-form

我使用bootstrap模式和Ruby on Rails的Simple_form gem。提交后一切正常。问题是当出现验证错误时,模式会在按下提交按钮时关闭。只有再次单击模态按钮时才会显示错误。

我需要最终用户在提交表单后看到错误消息 - 我一直在想如果有错误我可以使用javascript重新启动模式,但我无法弄清楚如何。任何帮助都会非常感谢。

我的模态如下:

      <%= link_to "Click here &raquo;".html_safe,
    "#myModal", data: { toggle: 'modal'}, class: "button-main", id: "button-contact" %>

    <!-- Modal -->
    <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Email Contact Form</h4>
          </div>
          <div class="modal-body">
            <%= simple_form_for @contact, url: contact_path, html: { id: "contact-form" } do |f| %>
                    <%= f.error_notification %>
            <%= f.input :name, error: 'Name is mandatory, please specify one' %>
            <%= f.input :email, label: 'Your Email' %>
                    <%= f.input :email_confirmation, label: 'Confirm your Email address' %>
                    <%= f.input :preferred_time, collection: ["Any time", "9am - 12pm Tuesday", "2pm - 6pm Tuesday", "9am - 12pm Wednesday"], selected: "Any time" %>
                    <%= f.input :subject, collection: ["New Appointment", "Request to change existing appointment", "Information", "Other"], selected: "New Appointment" %>
                        <%= f.input :message, as: :text %>

                <div class="modal-footer">
                <%= f.button :submit, "Send", 'data-disable-with' => "Sending...",  class: "submit-button", id: "modal-send" %>
                            <p class="warning">
                                Certain email providers have been known to mark our replies as spam, please be sure to check your junk mail inbox if awaiting a response
                            </p>
                <% end %>
          </div>
        </div>
      </div>
    </div>
</div>

和我的模特:

  class Contact

    include ActiveModel::Model
    include ActiveModel::Conversion
    include ActiveModel::Validations

    attr_accessor :name, :email, :email_confirmation, :message, :preferred_time, :subject

    validates :name,
      presence: true

    validates :email,
      presence: true, length:  { minimum: 6, maxium: 30 }, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }, confirmation: true

    validates :email_confirmation,
      presence: true, length:  { minimum: 6, maxium: 30 }, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }

    validates :message,
      presence: true,  length: { minimum: 6, maxium: 1000 }

  end

由于

1 个答案:

答案 0 :(得分:0)

我确定这不是最干净或最好的方法,但最终找到了一个解决方案,只有在出现错误时再次加载模态。刚刚使用模态将以下内容添加到我的页面。

<% if  @contact.errors.present?%>
    <script>
        $(window).load(function () {
            $('#myModal').modal('show');
            });
    </script>
<% end  %>