2016-11-23 37 views
0

我想知道如何在rails中合併循環。Rails 4:如何合併循環

我是否需要在控制器中執行該操作,或者甚至可以在視圖中執行該操作?
如果我想知道如何決定渲染哪個部分。

例如,我必須建立模型:itemreceipe

首先,我如何將所有食譜和項目合併到一個循環中?
其次,如果我想渲染每個receipe部分_receipe和每個item部分_item,我該怎麼做呢?

在此先感謝您的幫助!

回答

1

這應該工作開箱:

# in the controller 
items = Item.all   # some scope on Items 
receipes = Receipe.all  # some scope on Receipes 
@things = items + receipes # combine them into one variable 

# in the view 
render @things    # renders a collection and renders for each item in 
          # the array a partial named like the item's class name