1

3 つの異なるモデルのすべての要素を取得するコントローラー関数を作成しました。これと同じくらい簡単です:

def get_all_data
  @events = Event.all
  @activities = Activity.all
  @places = Place.all
end

次に get_all_data.json で:

json.partial! 'event', collection: @events
json.partial! 'activity', collection: @activities
json.partial! 'place', collection: @places

問題は、最後の 1 つのパーシャルのみをレンダリングすることです。何か不足していますか?これをもっと良い方法で行うことはできますか?

4

1 に答える 1

4

それぞれを JSON 構造の独自のメンバーに入れてみてください。

json.event do
  json.partial! 'event', collection: @events
end
json.activity do
  json.partial! 'activity', collection: @activities
end
json.place do
  json.partial! 'place', collection: @places
end

私はそれがうまくいくように感じます。

于 2014-01-29T16:00:46.787 に答える