我通常对每个资源有以下部分,例如对于用户
:
_users
包含table
定义以及thead
和tbody
_user
持有一个tr
使用这样的快捷方式渲染所有内容会很好:
= render @users
但遗憾的是,这没有考虑 _users
容器,所以我总是必须这样做:
= render 'users', users: @users
然后在 _users
部分中我可以执行以下操作:
= render users
这没有捷径吗?也许是这样的:
= render @users, wrapper: true
= render @users, wrapper: 'users'
最佳答案
不确定我是否正确,但我会尝试猜测。按照我理解你的问题的方式,我会这样解决它:
_users.html.erb
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<%= render @users %> # this will render _user.html.erb
</tbody>
</table>
_user.html.erb
<tr>
<td><%= user.name %></td>
</tr>
这应该有用,至少对我有用。如果我没有正确理解您的问题,请告诉我。
关于ruby-on-rails - rails : rendering collections as tr with a table around it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22120292/