breaks <form>

2019-09-05 21:20发布

问题:

I have the following code to generate a <form>

  <div class="modal-dialog">
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title" id="myModalLabel">My form</h4>
  </div>
  <div class="modal-body">
   <ul class="errors has-error"></ul>
   <p>Some text</p>
    <%= form_tag(some_path(@object), remote: true, id: "my-form") do %>
        <table class="table table-striped show">
          <tr>
            <th></th>
            <th>Some value</th>
            <th>Some value2</th>
            <th>Some value3</th>
            <th>Some value4</th>
            <th>Some value5</th>
            <th>Some value6</th>
            <th>Some value7</th>
          </tr>
            <% @object.each do |o| %>
              <tr>
                <td><div class="checkbox"><label><%= check_box :condition1, o.value1 %></label></div></td>
                <td><%= o.value2.strftime("%d.%m.%Y") %></td>
                <td><%= o.value3 %></td>
                <td><%= o.value4 %></td>
                <td><%= number_with_precision o.value5, :precision => 2 %></td>
                <td><%= number_with_precision o.value6, :precision => 2 %></td>
                <td><%= number_with_precision o.value7, :precision => 2 %></td>
                <td><%= number_with_precision o.value8.strftime("%d.%m.%Y") %></td>
              </tr>
            <% end %>


        </table>

    <p>Some text.</p>

  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
    <%= submit_tag "Submit", data: { disable_with: "Please wait..." }, :id => 'modal-submit', :class => "btn btn-primary" %>
    <% end %>
    </div>
</div>

Oddly, in the generated HTML code the submit button is put after the </form> tag. However, if I place the submit button before the first </div> after </table>, <input class="btn btn-primary" data-disable-with="Please wait..." id="modal-submit" name="commit" type="submit" value="Submit"> is within the form. Is this intended behaviour or a bug in the form helper?

this code works

    <p>Some text.</p>
    <%= submit_tag "Submit", data: { disable_with: "Please wait..." }, :id => 'modal-submit', :class => "btn btn-primary" %>
    <% end %>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
    </div>
</div>

Edit: This problem doesn't seem to be caused by Rails. The HTML code that gets send to the browser is actually correct. But the browser closes the form too early. This must therefore be invalid HTML, but I have no idea why.

回答1:

Apparently you are not allowed to start a form within a <div> without closing the form before the tag. I solved my problem by placing the <form> tag before <div class="modal-body">



回答2:

Take cate where to start a form and where to finish. Is this working?

<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times; </button>
  <h4 class="modal-title" id="myModalLabel">My form</h4>
</div>

<%= form_tag(some_path(@object), remote: true, id: "my-form") do %>

<div class="modal-body">
  <ul class="errors has-error"></ul>
  <p>Some text</p>
  <table class="table table-striped show">
    <tr>
      <th></th>
      <th>Some value</th>
      <th>Some value2</th>
      <th>Some value3</th>
      <th>Some value4</th>
      <th>Some value5</th>
      <th>Some value6</th>
      <th>Some value7</th>
    </tr>
    <% @object.each do |o| %>
    <tr>
      <td><div class="checkbox"><label><%= check_box :condition1, o.value1 %></label></div></td>
      <td><%= o.value2.strftime("%d.%m.%Y") %></td>
      <td><%= o.value3 %></td>
      <td><%= o.value4 %></td>
      <td><%= number_with_precision o.value5, :precision => 2 %></td>
      <td><%= number_with_precision o.value6, :precision => 2 %></td>
      <td><%= number_with_precision o.value7, :precision => 2 %></td>
      <td><%= number_with_precision o.value8.strftime("%d.%m.%Y") %></td>
    </tr>
    <% end %>
  </table>

  <p>Some text.</p>

</div>
<div class="modal-footer">
  <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<%= submit_tag "Submit", data: { disable_with: "Please wait..." }, :id => 'modal-submit', :class => "btn btn-primary" %>
</div>

<% end %>