我的控制器中有一些代码:
def index
@tasks = Task.paginate(page: params[:page], per_page: 12 )
@tasks_row = task_to_rows(@tasks)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @tasks }
end
end
task_to_rows:
def task_to_rows(tasks)
item_at_row = 0
task_row = Array.new
for i in (0..tasks.length)
item_at_row += 1
if item_at_row == 1
temp_arr = Array.new
temp_arr << tasks[i]
task_row << temp_arr
elsif item_at_row <= 3
temp_arr << tasks[i]
task_row << temp_arr
end
item_at_row = 0 if item_at_row == 3
end
return task_row
end
需要方法使数组的每个元素都是三个元素的数组。并且能够显示这样的信息:https ://www.evernote.com/shard/s8/sh/5dee1bbd-af05-4432-9e24-586de98f4452/a8229d8d3fd0bb222a76a927a70ee507
鉴于我有:
<% @tasks_row.each do |task3| %>
<h3>Row</h3>
<% task3.each do |task| %>
<%= task.name %>
<% end -%>
<% end -%>
但是 Rails 显示错误:
undefined method `name' for nil:NilClass
如果调试,则一切正常并显示。
更新: 在最终版本中,我只需要以这种格式显示数据:
<div class="row>
<div class="span3">Some data</div>
<div class="span3">Some data</div>
<div class="span3">Some data</div>
</div>
<div class="row">
<div class="span3">Some data</div>
<div class="span3">Some data</div>
<div class="span3">Some data</div>
</div>
.........