Rails - 如何一起引用当前和下一条记录

时间:2016-10-29 12:09:42

标签: ruby ruby-on-rails-4 ancestry

如何循环记录集并传递当前记录record1并仍能在同一操作中引用下一条记录?

        <% menu_item.children.menu.each do |record1| %>
            <%= wrap_submenu(record1) %>
            <%= wrap_submenu_a(record2) %>
        <% end %>

我正在使用祖先宝石

2 个答案:

答案 0 :(得分:1)

你可以使用each_cons(2):

   (1..10).each_cons(2){|a,b| puts a;puts b; puts "----"}
    =>
    1
    2
    ----
    2
    3
    ----
    3
    4
    ----
    4
    5
    ----
    5
    6
    ----
    6
    7
    ----
    7
    8
    ----
    8
    9
    ----
    9
    10
    ----

所以在你的情况下:

menu_item.children.menu.each_cons(2) do |record1,record2|
...

答案 1 :(得分:0)

不是很干净,但您可以将<div ng-controller="LoginCtrl"> <form ng-submit="login()"> <div> <label>Username</label> <input ng-model="username"> <label>Password</label> <input ng-model="password" type="password"> </div> <button type="submit">Submit</button> </form> </div> 用于最后一个元素迭代考虑