[英]Rails 3: Looping through array of objects, ignoring the first object in the array?
In my view i am trying to dispaly a table of objects, this is my code: 在我看来,我正在尝试分发对象表,这是我的代码:
<div id='categories_show'>
<table border="1">
<tr>
<th>Categories</th>
<th>CBB's</th>
</tr>
<% for category in @critical_process.categories %>
<tr>
<td rowspan="<%= category.capability_building_blocks.size %>"><%= category.category_title %></td>
<td><%= category.capability_building_blocks.first.cbb_title %></td>
</tr>
<% (category.capability_building_blocks - category.capability_building_blocks.first).each do |cbb| %>
<tr>
<td><%= cbb.cbb_title %></td>
</tr>
<% end %>
<% end %>
</table>
</div>
however this is throwing an error saying: can't convert CapabilityBuildingBlock into Array
但这抛出一个错误说:
can't convert CapabilityBuildingBlock into Array
the relationships are correct, the error is coming from the line where i try subtract the first object of the array here: <% (category.capability_building_blocks - category.capability_building_blocks.first).each do |cbb| %>
关系是正确的,错误来自于我尝试在此处减去数组第一个对象的行:
<% (category.capability_building_blocks - category.capability_building_blocks.first).each do |cbb| %>
<% (category.capability_building_blocks - category.capability_building_blocks.first).each do |cbb| %>
is there any way i can loop through the array ignoring the first object in the array? 有什么办法可以让我在数组中循环,而忽略数组中的第一个对象?
Thanks 谢谢
Try using Array.drop - http://www.ruby-doc.org/core/classes/Array.html#M000294 尝试使用Array.drop- http: //www.ruby-doc.org/core/classes/Array.html#M000294
<% category.capability_building_blocks.drop(1).each do |cbb| %>
<tr>
<td><%= cbb.cbb_title %></td>
</tr>
<% end %>
Additionally, this is more readable (And I'm 80% sure it works): 此外,这更具可读性(我80%确信它可以工作):
<%= category.capability_building_blocks[1..-1].each do |cbb| %>
You can use the built in slice operators to select any elements from an array that you want. 您可以使用内置的切片运算符从所需数组中选择任何元素。
-1
represents the last element in the array. -1
表示数组中的最后一个元素。
<%= (category.capability_building_blocks - [category.capability_building_blocks.first]).each do |cbb| %>
Also... 也...
stop_here = category.capability_building_blocks.length
category.capability_building_blocks[1..(stop_here)].each do |cbb|
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.