开发者

Rails 3 - tables in html.erb

开发者 https://www.devze.com 2023-03-19 22:17 出处:网络
I\'d like to view data from different database-tables in a view with tables like this picture shows: I\'m familiar with HTML tags <table>, <td> and <tr>, but I\'m having trouble wit

I'd like to view data from different database-tables in a view with tables like this picture shows:

Rails 3 - tables in html.erb

I'm familiar with HTML tags <table>, <td> and <tr>, but I'm having trouble with multiple queries in a column.

<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>
开发者_JAVA技巧

This code looks like this:

Rails 3 - tables in html.erb

As you can see, the skills, expected-qualifications, and current-qualifications repeat.

My question: How should the codes be ordered in the table so it will look the way I want it to?


Try 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 %>


if there is REALLY can be more than one skill, expected_qualification and current_qualification so you use has_many assosiation forposition

<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>

Otherwise you should use has_one association

0

精彩评论

暂无评论...
验证码 换一张
取 消