0

我正在尝试构建以下表格视图:

+----------+Actor 1+Actor 2+Actor 3+
+ REQ 1    +   X   +       +    X  +
+ REQ 2    +       +   X   +    X  +
+ REQ 3    +   X   +   X   +    X  +
+ REQ 4    +       +       +       +
+----------------------------------+

我目前有以下代码(虽然不确定构建它的方式):

<table class="table table-striped">
  <tr>
    <th></th>
    <% @project.actors.order('name').uniq.each do |actor| %>
      <th><%= actor.name %></th>
    <% end %>
  </tr>
  <% @project.requirements.order('position').each do |req, actors| %>
    <tr>
      <td><%= req.name_for_display %></td>
      <% actors.sort_by{|a| a.name}.each do |actor| %>
        <td><%= actor.name %></td>
      <% end %>
    </tr>
  <% end %>
</table>

引发异常:undefined method `sort_by' for nil:NilClass

谢谢你的帮助。

4

1 回答 1

1

已解决:这是工作代码

在控制器中

def method_name
  @project = Project.includes(:requirements => [:actors]).find(params[:id])
  @actors = @project.actors.index_by(&:id)
end

在视图中

<table class="table table-striped">
  <tr>
    <th></th>
    <% @actors.values.each do |actor| %>
      <th><%= actor.name.titleize %></th>
    <% end %>
  </tr>

  <% @project.requirements.order('position').each do |requirement, actors| %>
    <% hash = requirement.actors.index_by(&:id) %>
    <tr>
      <td><%= link_to requirement.name_for_display, project_requirement_path(@project, requirement) %></td>
      <% @actors.keys.each do |id| %>
        <td><%= hash[id].present? ? "X" : nil %></td>
      <% end %>
    </tr>
  <% end %>
</table>
于 2013-05-16T18:13:08.110 回答