0

我创建了一个呈现数据表视图的单元格,我想将它重用于其他表视图。对于该数据的每一行,我希望将一些独特的元素与标准列混合。现在,我的 Cell 的简化部分版本show.html.slim如下所示:

- users.each do |user|
  tr
    / A column unique to User
    td
      = link_to_if current_user == user, "Groups", user_groups_path(user)
    / Common code that can be shared across other tables
    td user.name

我希望能够从这个级别提取用户特定的代码并从上面注入它。细胞文档指出:

如果有疑问,请将视图的嵌套部分封装到单独的单元格中。您可以在单元格中使用 #cell 方法来实例化嵌套单元格。

但是,很难简单地实例化一个新的 Cell (编辑:在表格单元格内):

  1. 我需要知道要创建什么单元格。
  2. 我需要能够从父单元格传递信息(user在我的示例中)。
  3. 我需要能够传递特定于子单元格的信息(current_user在我的示例中)。

将这种类型的部分渲染注入 Cell 有哪些选择?它们中的任何一个是通常首选的解决方案吗?

4

1 回答 1

0

我最终只是在我的单元格上定义了一些接受块的方法

class DataTableCell < Cell::ViewModel
  def row_prefix(&blk)
    @_row_prefix = blk
  end

  def render_row_prefix(item)
    return unless @_row_prefix
    @_row_prefix.call(item).call
  end
end

每当我想渲染 时DataTableCell,我都会提供如下块:

cell = cell(:data_table)
cell.row_prefix do |item|
  cell(:users_row_prefix, user: item, current_user: current_user)
end

在我的牢房里,我可以打电话render_row_prefix(row_item)

我不接受这种解决方案,因为它可能有点重量级,但它确实有效。

于 2014-09-22T19:03:19.717 回答