迭代列名

时间:2015-06-09 20:06:35

标签: ruby-on-rails ruby-on-rails-4

我有一个employee对象,我只想在show.html.erb文件中显示密钥列表和相关值。这是我目前所拥有的,但如何通过for循环完成此操作?

<table class="table table-striped">
<tr>
  <th>Item</th>
  <th>Value</th>
</tr>
<tr>
  <td>Name</td>
  <td><%= @employee.name %></td>
</tr>
<tr>
  <td>Email</td>
  <td><%= @employee.email %></td>
</tr>
<tr>
  <td>Job Code</td>
  <td><%= @employee.job_code %></td>
</tr>
...
</table>

1 个答案:

答案 0 :(得分:2)

<% @employee.attributes.each do |key, value| %>
  <tr>
    <td><%= key.gsub('_', ' ').titleize %></td>
    <td><%= value %></td>
  </tr>
<% end %>