【问题标题】:Rails Simple Form <form> tags not wrapping around controlsRails 简单表单 <form> 标签不环绕控件
【发布时间】:2014-01-08 12:21:12
【问题描述】:

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

下面是被调用的控制器更新方法的代码。这目前不运行。

  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 %>");

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

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

【问题讨论】:

    标签: javascript jquery ruby-on-rails-4 simple-form


    【解决方案1】:

    错误来自您尝试在禁止的 &lt;tr&gt; 元素内直接声明的 &lt;form&gt; 元素。

    你只有两个选择:

    • &lt;form&gt; 包裹在整个桌子周围

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

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

    【讨论】:

    • 感谢 Orbig。很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 2018-02-02
    相关资源
    最近更新 更多