Rails 3 - html.erb中的表

时间:2011-07-11 11:59:08

标签: html ruby-on-rails-3 html-table

我想在视图中查看来自不同数据库表的数据,如下图所示:enter image description here

我熟悉HTML代码<table><td><tr>,但我在列中遇到多个查询时遇到问题。

<table>
  <tr>
    <th>Skills &nbsp; &nbsp; &nbsp;</th>
    <th>Expected-qualifications</th>
    <th>Current-qualifications</th>
  </tr>

   <% @employee.position.skills.each do |skill| %><% @employee.position.expected_qualifications.each do |expected_qualification| %><% @employee.current_qualifications.each do |current_qualification| %>
  <tr>
    <td><%= skill.kategorien %></td>
    <td><%= expected_qualification.sollqualifikation %></td>
    <td><%= current_qualification.istqualifikation %></td>
  </tr>
  <% end %><% end %><% end %>

</table>

此代码如下所示:

enter image description here

如您所见,技能,预期资格和当前资格重复。

我的问题:如何在表格中订购代码,以便它看起来像我想要的那样?

2 个答案:

答案 0 :(得分:5)

尝试zip

<% @employee.position.skills.zip(@employee.position.expected_qualifications,@employee.current_qualifications).each |skill expected_qualification current_qualification| %>
<tr>
  <td><%= skill.kategorien %></td>
  <td><%= expected_qualification.sollqualifikation %></td>
  <td><%= current_qualification.istqualifikation %></td>
</tr>
<% end %>

答案 1 :(得分:1)

如果确实可以有多个skillexpected_qualificationcurrent_qualification,那么您对has_many使用position分析

<tr>
  <td><%= @employee.position.skills.map(&:kategorien).join(", ") %></td>
  <td><%= @employee.position.expected_qualifications.map(&:sollqualifikation).join(", ") %></td>
  <td><%= @employee.current_qualifications.map(&:istqualifikation).join(", ") %></td>
</tr>

否则您应该使用has_one关联