我有以下部分:軌道4:渲染部分具有可變
<tr class="profile-row">
<td class="profile-header"><%= attribute %></td>
<% for week in @weeks %>
<td><%= week.<%= field %> %></td> <!-- where it fails -->
<% end %>
</tr>
...我想能夠提供2個變量,attribute
和field
。當我嘗試呈現局部有以下:
<%= render 'foo', attribute: 'Current Weight', field: 'current_weight' %>
...我想:
<tr class="profile-row">
<td class="profile-header">Current Weight</td>
<% for week in @weeks %>
<td><%= week.current_weight %></td> <!-- where it fails -->
<% end %>
</tr>
...但這個失敗syntax error, unexpected tOP_ASGN...
。我知道這不是提供變量的正確方法,但我應該怎麼做?