关于javascript:Rails简单表单<form>标签不环绕控件

Rails Simple Form <form> tags not wrapping around controls

我已经在Ruby on Rails 4中使用Simple_form创建了部分文档,但是当我渲染页面时,部分文档中的表单标签不会环绕控件,因此当我单击"提交"按钮时,表单不会发布。

这是浏览器中呈现的部分内容的HTML源。您会注意到控件不在结束表单标记内。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<tr data-id="4">
<form id="edit_social_platform_4" class="simple_form edit_social_platform" novalidate="novalidate" method="post" data-remote="true" action="/social_platforms/4" accept-charset="UTF-8"></form>
<td>

</td>
<td>

</td>
<td>

</td>
<td>
<input class="btn" type="submit" value="Update Social platform" name="commit">
</td>
</tr>

这是rails中的局部代码:

1
2
3
4
5
6
7
8
<%= simple_form_for(@social_platform, remote: true) do |f| %>

    <td><%= f.input :name %></td><br/>
    <td><%= f.input :url %></td><br/>
    <td><%= f.input :user_id %></td><br/>
    <td><%= f.button :submit %></td>

<% end %>

以下是所调用的控制器更新方法的代码。当前无法运行。

1
2
3
4
5
6
7
8
9
  def update
    respond_to do |format|
      if @social_platform.update(social_platform_params)
        format.html { redirect_to @social_platform, notice: 'Social platform was successfully updated.' }
        format.js {}
        format.html { render action: 'edit' }
      end
    end
  end

这是将部分内容呈现到页面上的update.js.erb文件:

1
$('tr[data-id=<%= @social_platform.id %>]').replaceWith("<%=j render 'social_platforms/social_platforms', social_platform: @social_platform %>");

下面是部分与之交互的视图上的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<table class="table socialPlatformTable">
  <thead>
  <tr>
    <th>Name</th>
    <th>Url</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
  </thead>

  <tbody class="social-platforms">
  <% @social_platforms.each do |social_platform| %>
      <tr data-id="<%= social_platform.id %>">
        <td><%= social_platform.name %></td>
        <td><%= social_platform.url %></td>
        <td><%= link_to 'Edit', edit_social_platform_path(social_platform), remote: true %></td>
        <td><%= link_to 'Destroy', social_platform, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
  <% end %>
  </tbody>
</table>

错误来自<form>元素,您尝试直接在<tr>元素内声明此元素。

您只有两种选择:

  • <form>包裹在整个表中

    1
    2
    3
    4
    5
    <%= simple_form_for(@social_platform, remote: true) do |f| %>
      <table>
      ....
      </table>
    <% end %>
  • 将其放入单个表单元格中

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <table>...
      <tr>
      <td colspan="4">
        <%= simple_form_for(@social_platform, remote: true) do |f| %>
          ...
        <% end %>
      </td>
      </tr>
    </table>

实际上,无法(afaik)将表格package在几个(但不是全部)表格单元周围。 (如果有的话,我很高兴知道它)