通过 rails 部分生成具有独特 headers 和单元格的 table
Produce a table with unique headers and cells through a rails partials
我有一个标准的 table 布局,我想在我的 4 种不同资源的索引视图上使用它。我创建了一个 index_table 部分,它使用本地人提供:
- Table 标题
- Table headers
- Tables objects(比如@users)
- Table 个细胞
<%= render 'shared/index_table',
scope_title: 'All Debts',
table_headers: ['ID', 'Debtor', 'Amount', 'Status', 'Stage', ''],
table_objects: @debts,
table_cells: ['id', 'debtor.name', 'amount', 'status', 'stage'] %>
除了 table 单元格之外的所有单元格都在工作,自然地,我 运行 遇到的问题是单元格显示为 Object.id,而不是实际的 object id.
<table class="table table-striped table-bordered table-hover" id="sample_3">
<thead>
<tr>
<% table_headers.each do |table_header| %>
<th> <%= table_header %> </th>
<% end %>
</tr>
</thead>
<tbody>
<% table_objects.each do |object| %>
<tr>
<% table_cells.each do |cell| %>
<td> <%= object %>.<%= cell %> </td>
<% end %>
<td>
<a href="javascript:;" class="btn btn-outline btn-sm blue">
<i class="fa fa-share"></i> View </a>
<a href="javascript:;" class="btn btn-outline btn-sm green-jungle">
<i class="fa fa-edit"></i> Edit </a>
<a href="javascript:;" class="btn btn-outline btn-sm red-mint">
<i class="fa fa-trash"></i> Delete </a>
</td>
</tr>
<% end %>
</tbody>
</table>
如有任何意见,我们将不胜感激。
还有没有更优雅的方法,或者这是什至建议的方法?
感谢您的帮助!
改变
<td> <%= object %>.<%= cell %> </td>
到
<td> <%= object.send(cell) %> </td>
编辑
我试图轻松地回答您的问题,但我会更深入地探讨您应该如何做这整件事。您最初设置它的方式并不完全 "proper" 并且很可能是这个问题被否决的原因。
第一件事是您不想通过局部变量提供所有这些变量。它变得很麻烦,视图不是这个逻辑的地方。您知道要发送到局部模型的模型列表,因此只需将其传递给您调用局部模型的所有 4 种情况。想出一个名称来封装所有 4 种类型的对象所代表的内容,但对于示例,我将其称为 objects
,就像您使用的那样。
<%= render 'shared/index_table', objects: @debts %>
现在在你的模型中,为你想在部分中显示的每个属性创建一个显示方法。
def self.scope_title
'All Debts'
end
def self.table_headers
['ID', 'Debtor', 'Amount', 'Status', 'Stage', '']
end
def table_cells
[id, debtor.name, amount, status, stage]
end
在你的模型中加入这些可以做一些事情。
- 你可以测试这些方法
- 您可以在属于它的地方编写自定义逻辑
- 字段将被评估,就像它们前面有一个
self.
一样。这意味着您的 debtor.name
字段将 return 它在数组中的值。
关于最后一点,您还可以在此处检查 debtor
是否存在,以便在没有关系时不会中断:debtor.try(:name)
现在在你的视野中
<% objects.first.class.table_headers.each do |table_header| %>
<% objects.each do |object| %>
...
<% object.table_cells.each do |cell| %>
<td> <%= cell %> </td>
超出此问题范围的最后 2 个优化:
- 创建一个超类
class ObjectDisplay
(改名Object)并将这些显示方法作为存根放入其中。
- 为每种类型的对象制作显示类,并在其中制作显示方法。
class DebtDisplay < ObjectDisplay
我有一个标准的 table 布局,我想在我的 4 种不同资源的索引视图上使用它。我创建了一个 index_table 部分,它使用本地人提供:
- Table 标题
- Table headers
- Tables objects(比如@users)
- Table 个细胞
<%= render 'shared/index_table',
scope_title: 'All Debts',
table_headers: ['ID', 'Debtor', 'Amount', 'Status', 'Stage', ''],
table_objects: @debts,
table_cells: ['id', 'debtor.name', 'amount', 'status', 'stage'] %>
除了 table 单元格之外的所有单元格都在工作,自然地,我 运行 遇到的问题是单元格显示为 Object.id,而不是实际的 object id.
<table class="table table-striped table-bordered table-hover" id="sample_3">
<thead>
<tr>
<% table_headers.each do |table_header| %>
<th> <%= table_header %> </th>
<% end %>
</tr>
</thead>
<tbody>
<% table_objects.each do |object| %>
<tr>
<% table_cells.each do |cell| %>
<td> <%= object %>.<%= cell %> </td>
<% end %>
<td>
<a href="javascript:;" class="btn btn-outline btn-sm blue">
<i class="fa fa-share"></i> View </a>
<a href="javascript:;" class="btn btn-outline btn-sm green-jungle">
<i class="fa fa-edit"></i> Edit </a>
<a href="javascript:;" class="btn btn-outline btn-sm red-mint">
<i class="fa fa-trash"></i> Delete </a>
</td>
</tr>
<% end %>
</tbody>
</table>
如有任何意见,我们将不胜感激。
还有没有更优雅的方法,或者这是什至建议的方法?
感谢您的帮助!
改变
<td> <%= object %>.<%= cell %> </td>
到
<td> <%= object.send(cell) %> </td>
编辑
我试图轻松地回答您的问题,但我会更深入地探讨您应该如何做这整件事。您最初设置它的方式并不完全 "proper" 并且很可能是这个问题被否决的原因。
第一件事是您不想通过局部变量提供所有这些变量。它变得很麻烦,视图不是这个逻辑的地方。您知道要发送到局部模型的模型列表,因此只需将其传递给您调用局部模型的所有 4 种情况。想出一个名称来封装所有 4 种类型的对象所代表的内容,但对于示例,我将其称为 objects
,就像您使用的那样。
<%= render 'shared/index_table', objects: @debts %>
现在在你的模型中,为你想在部分中显示的每个属性创建一个显示方法。
def self.scope_title
'All Debts'
end
def self.table_headers
['ID', 'Debtor', 'Amount', 'Status', 'Stage', '']
end
def table_cells
[id, debtor.name, amount, status, stage]
end
在你的模型中加入这些可以做一些事情。
- 你可以测试这些方法
- 您可以在属于它的地方编写自定义逻辑
- 字段将被评估,就像它们前面有一个
self.
一样。这意味着您的debtor.name
字段将 return 它在数组中的值。
关于最后一点,您还可以在此处检查 debtor
是否存在,以便在没有关系时不会中断:debtor.try(:name)
现在在你的视野中
<% objects.first.class.table_headers.each do |table_header| %>
<% objects.each do |object| %>
...
<% object.table_cells.each do |cell| %>
<td> <%= cell %> </td>
超出此问题范围的最后 2 个优化:
- 创建一个超类
class ObjectDisplay
(改名Object)并将这些显示方法作为存根放入其中。 - 为每种类型的对象制作显示类,并在其中制作显示方法。
class DebtDisplay < ObjectDisplay