我正在尝试创建一个基于特定模型的所有记录填充的表。
这是index.html.erb文件
<%= div_for(@departments, :class => "test") do |department| %>
<tr>
<td class="indexc1">
<%= department.name %>
</td>
<td class="indexc2">
<%= department.location %>
</td>
<td class="indexc3">
<%= department.date_completed %>
</td>
<td class="indexc4">
<ul class="action">
<li><%= link_to 'Edit Info', edit_department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Edit Tasks', department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Destroy', department, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"tlink3" %></li>
</ul>
</td>
</tr>
<% end %>
这似乎可以正确创建表行,但不会按预期将它们放入div中。
我希望能够为每个部门创建一个表行,然后能够使用AJAX添加或删除它们。
答案 0 :(得分:2)
如果你想将每一行包装在div中,你需要将div类和id添加到tr标签,IE:(tr id =“myid”class =“class”)而不是包装div周围的div。
您可以使用rails content_tag_for来生成:
<% @departments.each do |department| %>
<%= content_tag_for(:tr, department, :class => "test") do %>
<td class="indexc1">
<%= department.name %>
</td>
<td class="indexc2">
<%= department.location %>
</td>
<td class="indexc3">
<%= department.date_completed %>
</td>
<td class="indexc4">
<ul class="action">
<li><%= link_to 'Edit Info', edit_department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Edit Tasks', department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Destroy', department, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"tlink3" %></li>
</ul>
</td>
<% end %>
<% end %>