如何在 Ruby on Rails 站点的 HTML 表的左右列中显示数据



在我的Ruby on Rails应用程序中,我想在HTML表的左列中显示数据库中一个表中的一半数据,在右列中显示另一半。我只能显示左单元格中 id 为 1 的数据,然后在同一行的右单元格中显示数据,然后在左单元格中显示 id 为 2 的数据,然后在下一行的右单元格中再次显示:

<table>
    <% @categories.each do |category| %>
        <tr>
            <% if category.id == 1  %>
                <td bgcolor="#03FF00">
                    <%= category.id %>
                </td>
                <td bgcolor="#f1f1c1">
                    <%= link_to 'Edit', edit_category_path(category) %>&nbsp;|
                    <%= link_to 'Show', category_path(category) %>&nbsp;|
                    <%= link_to 'Destroy', category_path(category), method: :delete, data: { confirm: 'Are you sure?' } %>  
                </td>
            <% end %>
            <% if category.id == 1  %>
                <td bgcolor="#03FF00">
                    <%= category.id %>
                </td>
                <td bgcolor="#f1f1c1">
                    <%= link_to 'Edit', edit_category_path(category) %>&nbsp;|
                    <%= link_to 'Show', category_path(category) %>&nbsp;|
                    <%= link_to 'Destroy', category_path(category), method: :delete, data: { confirm: 'Are you sure?' } %>  
                </td>
            <% end %>                               
            <% if category.id == 2  %>
                <td bgcolor="#03FF00">
                    <%= category.id %>
                </td>
                <td bgcolor="#f1f1c1">
                    <%= link_to 'Edit', edit_category_path(category) %>&nbsp;|
                    <%= link_to 'Show', category_path(category) %>&nbsp;|
                    <%= link_to 'Destroy', category_path(category), method: :delete, data: { confirm: 'Are you sure?' } %>
                </td>
            <% end %>
            <% if category.id == 2 %>
                <td bgcolor="#03FF00">
                    <%= category.id %>
                </td>
                <td bgcolor="#f1f1c1">
                    <%= link_to 'Edit', edit_category_path(category) %>&nbsp;|
                    <%= link_to 'Show', category_path(category) %>&nbsp;|
                    <%= link_to 'Destroy', category_path(category), method: :delete, data: { confirm: 'Are you sure?' } %>  
                </td>
            <% end %>
        </tr>   
    <% end %>                           
</table>    

我已经尝试了很长时间,那段代码是我能想到的最好的代码,但是当我尝试使用它在一侧显示一半,在另一侧显示一半时不起作用,我试图使用它:

if category.id <= Category.count / 2

如果 category.id>类别计数/2

我知道这本身不是一种非常有效或"安全"的计算方式,因为使用了 id 而不是行号。

在我的类别表中,我有两列:id 和流派,但我只想显示每个类别的流派和链接:编辑、显示和销毁每个类别。

有什么想法吗?请尝试向我展示代码,而不是告诉我在哪里放置代码。

我假设您希望在页面上分两部分显示相同的表格行。所以我建议你做两张桌子,一张给左边,一张给右边。并玩 css。在左边的表格中给出:

float: left

在右边的表格中给出:

float: right

你也可以玩css的位置和其他属性。或者下一个选项是制作四列,但我不确定,因为您的问题有点不清楚。

或者,如果您只需要计算,请使用偶数和奇数。Ruby 提供了检查偶数和奇数的方法。例如:

a = 1
a.odd?
a.even?

更新:

左右浮动的代码片段:

<html>
    <head>
        <style type="text/css">
            .left{
                float: left;
            }
            /*Optional*/
            .right{
                float: right;
            }
        </style>
    </head>
    <body>
        <table class="left">
            <thead>
                <th>Name</th>
                <th>Action</th>
            </thead>
            <tbody>
                <td>Deep</td>
                <td><a href="#">Edit</a></td>
            </tbody>
        </table>
        <table class="right">
            <thead>
                <th>Name</th>
                <th>Action</th>
            </thead>
            <tbody>
                <td>Deep</td>
                <td><a href="#">Edit</a></td>
            </tbody>
        </table>
    </body>
</html>

最新更新