2016-11-23 14 views
0

レール内でループをマージする方法が不思議でした。Rails 4:ループをマージする方法

私はコントローラでそれを行う必要がありますか、それともビューでも可能ですか?
どの部分をレンダリングするかを決める方法がわかります。

たとえば、私はitemreceipeのモデルを用意しなければなりません。

まず、すべてのレシピとアイテムを1つのループにマージするにはどうすればよいですか?
第二に、それぞれreceipeの部分を_receipeと、それぞれitemの部分_itemについてレンダリングしたいのですが、どのように私はそれをdaとするのですか?

事前に各ヘルプをお寄せいただきありがとうございます。

答えて

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 
関連する問題