In one of my application, there is a table, Product with more than 100 columns. I wanted to iterate over all the columns and display their values on product show page.
This is how I did it:
<table border="1">
<% for column in Product.content_columns %>
<tr>
<th><%= column.human_name %></th>
<td><%= h @product.send(column.name) %> <td>
</tr>
<% end %>
</table>
This is how I did it:
<table border="1">
<% for column in Product.content_columns %>
<tr>
<th><%= column.human_name %></th>
<td><%= h @product.send(column.name) %> <td>
</tr>
<% end %>
</table>
column.human_name - Returns the human name of the column name. If your column is "product_owner", it will return "Product owner".
column.name will return the name of the column (product_owner)
@product.send(column.name) will return the value of the column.
No comments:
Post a Comment