我想知道如何在rails中合并循环。
我需要在控制器中这样做吗,或者在视图中也可以这样做吗?
如果我想知道如何决定渲染哪个部分。
例如,我必须建模:item和receipe。
首先,如何将所有食谱和项目合并到一个循环中?
其次,如果我想为每个receipe呈现部分_receipe,并为每个item呈现部分_item,我应该怎么做呢?
提前感谢您的每一次帮助!
发布于 2016-11-24 02:06:34
这应该是开箱即用的:
# 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 namehttps://stackoverflow.com/questions/40769951
复制相似问题