首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 4:如何合并循环

Rails 4:如何合并循环
EN

Stack Overflow用户
提问于 2016-11-24 00:43:50
回答 1查看 139关注 0票数 0

我想知道如何在rails中合并循环。

我需要在控制器中这样做吗,或者在视图中也可以这样做吗?

如果我想知道如何决定渲染哪个部分。

例如,我必须建模:itemreceipe

首先,如何将所有食谱和项目合并到一个循环中?

其次,如果我想为每个receipe呈现部分_receipe,并为每个item呈现部分_item,我应该怎么做呢?

提前感谢您的每一次帮助!

EN

回答 1

Stack Overflow用户

发布于 2016-11-24 02:06:34

这应该是开箱即用的:

代码语言:javascript
复制
# 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40769951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档