【问题标题】:Rails 6.1 Link submit_tag to form_withRails 6.1 将 submit_tag 链接到 form_with
【发布时间】:2021-10-26 20:14:01
【问题描述】:

我有一个 html 表,其中的表行是通过部分生成的。部分包含在 form_with 中,因此我可以使用复选框删除多条记录。问题是我希望我的 button_tag 在 form_with 块之外。是否可以将 button_tag 链接到表单,使其不必位于表单块内?

下拉操作菜单

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="actionMenuToggle" data-bs-toggle="dropdown" aria-expanded="false">Actions</button>
  <ul class="dropdown-menu" aria-labelledby="actionMenu">
    <li><%= button_tag "Delete Selected2", class: "btn btn-link dropdown-item", data: { action: "form-submission#destroyMultiple" } %></li>
  </ul>
</div>

然后,我像往常一样编码我的表格骨架,我的行呈现以下部分

<%= render partial: "client_table", clients: @clients %>

在那部分我有我的 form_with 标记并循环遍历客户端

<tbody id="clients">
<%= form_with(url: destroy_multiple_clients_path, data: { controller: 'form-submission', form_submission_target: "delete_multiple_form" }) do |form| %>
  <% @clients.each do |client| %>
    <tr>
      <td><%= check_box_tag("client_ids[]", client.id, false, { class: "form-check-input", onclick: "onClientSelect();" }) %></td>
      <td><%= client.id %></td>
      <td><%= client.name %></td>
      <td><%= client.city %></td>
      <td><%= client.state_province %></td>
      <td><%= client.postal_code %></td>
      <td><%= link_to 'Edit', edit_client_path(client), class: "btn btn-success edit_client" %> </td>
    </tr>
  <% end %>
<% end %>
</tbody>

【问题讨论】:

    标签: html ruby-on-rails


    【解决方案1】:

    只需将form: 'id_of_form' 添加到提交标签,然后在form_with .... id:'id_of_form' 上添加一个id

    【讨论】:

      【解决方案2】:

      表单不能是table, tbody or tr 的子级。

      因此,您需要将整个表格包装在 form_with 块中。如果是这种情况,将按钮包含在表单中可能会更容易,只需将表格和表格标题元素包装在表单中即可。

      (但请注意,您不能嵌套表单对象)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-29
        • 2017-10-07
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        • 1970-01-01
        • 2011-03-08
        • 2018-12-06
        相关资源
        最近更新 更多