【问题标题】:Each nested attributes in a table in RailsRails 表中的每个嵌套属性
【发布时间】:2018-03-29 14:50:00
【问题描述】:

我有一个带有嵌套属性的表单,现在我想在表格上显示,但我无法打破它作为列交互的行。我能怎么做?我能做什么?

Here's an image of my current table.

<% @salmonella.process_salmonellas.each do |process| %>
<tr align="center">
    <td><%= process.title %></td>
    <table>
        <% process.stages.each do |stage| %>
            <tr>
                <td><%= stage.title %></td>
                <td>
                    <table>
                        <% stage.lines.each do |line| %>
                            <tr>
                                <td><%= line.material %></td>
                                <td><%= line.indicator_name %></td>
                            </tr>
                        <% end %>
                    </table>
                </td>
            </tr>
        <% end %>
    </table>
</tr>

And this is how it should look like

这或多或少是我在脑海中绘制的表格模板:

 <table>
    <!-- Each for principal object -->
    <tr>
        <td><!-- fields of principal object --></td>
        <td>
            <!-- each nested attributes from principal object and the model -->
            <tr>
                <td><!-- fields of second object--></td>
                <td>
                    <!-- each from model and another model that is nested too from principal object and model -->
                    <tr>
                        <td><!-- fields of third object --></td>
                    </tr>
                </td>
            </tr>
        </td>
    </tr>
</table>

这就是它在我的控制器中的嵌套方式:

def salmonella_params
  params.require(:salmonella).permit(:title,
    process_salmonellas_attributes: [
      :id,
      :title,
      :_destroy,
      stages_attributes: [
        :id,
        :title,
        :_destroy,
        lines_attributes:[
          :id,
          :material,
          :indicator_name,
          :_destroy
        ]
      ]
    ])
end

【问题讨论】:

  • 你的意思是你没有行?原因是因为您只使用&lt;td&gt; 而没有使用&lt;tr&gt;(代表表格行)。如果是这种情况,请快速阅读w3schools.com/html/html_tables.asp
  • 我尝试输入&lt;tr&gt;,但它无法识别为&lt;td&gt;
  • 那是因为您必须将 &lt;td&gt; 放在 &lt;tr&gt; 内(以及 &lt;table&gt; 内),请观看我发送的链接中的一个示例。
  • 3 张桌子?我会更新问题
  • 用 3 张桌子搞得一团糟

标签: html css ruby-on-rails


【解决方案1】:

我按照 Jeremy 的建议做了,看起来像这样:

<% @salmonella.process_salmonellas.each do |process| %>
    <tr>
        <% process.stages.each do |cont| $b = cont.lines.count  end %>
        <td rowspan="<%= $b %>"><%= process.title %></td>
        <% process.stages.each do |stage| %>
        <td rowspan="<%= stage.lines.count %>"><%= stage.title %></td>
            <% stage.lines.each_with_index do |line, o| %>
                <% if o == 0 %>
                    <td><%= line.material %></td>
                    <td><%= line.indicator_name %></td>
                <% else %>
                    <tr>
                        <td><%= line.material %></td>
                        <td><%= line.indicator_name %></td>
                    </tr>
            <% end %>
        <% end %>
    </tr>
<% end %>

现在它的工作方式与图像的工作方式相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多