Rails is adding a </form> tag in a spot that is not where I closed the do block, it is breaking my form

橙三吉。 提交于 2021-01-27 04:57:07

问题


I'm having one heck of an issue working out how to do this. I have 2 forms that need to be displayed next to each other but you can't embed forms, I got around this by just putting the 2nd form after the first one.

However, rails is ignoring my <% end %> tag for the 2nd form and it is auto-closing the 2nd form before it needs to be closed. This is making my form not work.

Here's the view with some bits omitted for brevity.

<div class="row">
  <div class="col-md-3">
    <div class="well">
      <%= render "shared/some_form", url_path: foo_path %>
      <%= form_tag some_other_path, method: :delete, id: "form_bulk_action", role: "form" do %>
      <div id="bulk_action_group">
        <div class="form-group">
          <%= label_tag "perform_bulk_action", "For each checked item" %>
          <%= select_tag "perform_bulk_action", options_for_select([ ["Delete", "destroy"] ]), class: "form-control" %>
        </div>
        <%= button_tag id: "foo" %>
      </div>
    </div>
  </div>
  <div class="col-md-9">
    <table class="table table-condensed table-hover">
      <thead>
        <tr>
          <th><%= check_box_tag :check_all_data_rows, "", false %></th>
        </tr>
      </thead>
      <tbody>
        <% @foo.each do |foo| %>
          <tr>
            <td><%= check_box_tag "bulk_ids[]", foo.id, false, %></td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
</div>
<% end %>

The partial that is loading some_form is the first form. The beginning and end of that form happens inside of the partial.

The second form is the form_tag. As you can see from the html markup we have a sidebar on the left and the main contents on the right being loaded in a table.

The idea of the 2nd form is it allows you to check off rows and then perform a bulk action on the checked off items. Similar to how you might mark 15 e-mails in gmail and then delete them.

The problem is rails is ignoring the <% end %> at the bottom of that code snippet and instead it's injecting a </form> right after the button with an id: foo.

This is causing the bulk_ids value to always be nil because at that point the form is already closed and they are not part of the form. The crazy thing is it only fails when you goto the page from another page (ie. turbolinks). It works when you do a full load of the page, however I think the main issue is the premature closing of the </form> tag.

How can I set this all up so the forms are not embedded and the 2nd form closes all the way at the bottom of the table while still using the form helpers?


回答1:


Your root problem appears to be forms are both actions and rectangles. So you need a little rectangle inside a bigger one, each with its own, different action. A CSS expert might know how to write a <form style="display:inline" ...> such that one form can flow around another, without enforcing a rectangular shape.




回答2:


I believe the problem is that the form originates with the render shared/some_form call that is inside the , but the code to close the form is outside that div and after a table. If you move the start of the form to above all the divs shown here and have all the divs and tables reside inside the form, then it should work.

My understanding is that a form is not meant to operate correctly half inside another element and thus a form should either be within or outside other elements, not partially.

Here is a little more information about forms and tables (and divs): Form inside a table



来源:https://stackoverflow.com/questions/19736609/rails-is-adding-a-form-tag-in-a-spot-that-is-not-where-i-closed-the-do-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!