gpt4 book ai didi

javascript - Rails Simple Form
标签未环绕控件

转载 作者:行者123 更新时间:2023-12-02 17:55:26 25 4
gpt4 key购买 nike

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

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

<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>
<div class="control-group string required social_platform_name">
</td>
<td>
<div class="control-group url optional social_platform_url">
</td>
<td>
<div class="control-group integer optional social_platform_user_id">
</td>
<td>
<input class="btn" type="submit" value="Update Social platform" name="commit">
</td>
</tr>

这是rails中部分的代码:

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

下面是正在调用的 Controller 更新方法的代码。目前尚未运行。

  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 文件:

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

下面是与部分交互的 View 上的代码:

<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>围绕整个 table

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

    <table>...
    <tr>
    <td colspan="4">
    <%= simple_form_for(@social_platform, remote: true) do |f| %>
    ...
    <% end %>
    </td>
    </tr>
    </table>

实际上,(据我所知)没有办法将表单包裹在多个(但不是全部)表格单元格中。 (如果有的话,我很高兴知道)

关于javascript - Rails Simple Form <form> 标签未环绕控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20995524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com