Rails正在添加</form>标记的位置不在我关闭do块的位置,这破坏了我的表单 [英] Rails is adding a </form> tag in a spot that is not where I closed the do block, it is breaking my form

查看:53
本文介绍了Rails正在添加</form>标记的位置不在我关闭do块的位置,这破坏了我的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,需要解决该问题.我有2个需要彼此相邻显示的表单,但是您不能嵌入表单,我通过将第二个表单放在第一个表单之后来解决这个问题.

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.

但是,rails忽略了第二种形式的<%end%> 标记,并且它在需要关闭之前自动关闭了第二种形式.这使我的表单不起作用.

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 %>

正在加载some_form的局部是第一个表单.这种形式的开始和结束发生在部分内容的内部.

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

第二种形式是form_tag.正如您从html标记中看到的那样,我们在表的左侧有一个侧边栏,而在右侧则有一个主要内容.

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.

第二种形式的想法是,它允许您检查行,然后对被检查的项目执行批量操作.类似于您可以在gmail中标记15封电子邮件,然后将其删除的方法.

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.

问题是Rails忽略了该代码段底部的<%end%> ,而是在其后注入</form> 具有 id:foo 的按钮.

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.

这将导致bulk_ids值始终为nil,因为此时表单已关闭并且它们不属于表单.疯狂的事情是,只有当您从另一个页面转到该页面(即,turbolinks)时,它才会失败.当您完成页面的全部加载时,它会起作用,但是我认为主要问题是</form> 标记的过早关闭.

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?

推荐答案

我认为问题在于表单是由内的render shared/some_form调用产生的,但是关闭表单的代码不在div和在桌子后面.如果将表单的开始位置移到此处显示的所有div上方,并且所有div和表都位于表单内,则它应该可以工作.

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.

以下是有关表单和表格(以及div)的更多信息:表格内的表格

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

这篇关于Rails正在添加&lt;/form&gt;标记的位置不在我关闭do块的位置,这破坏了我的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆