1

我创建了一个控制器函数来获取三个不同模型的所有元素。就这么简单:

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

问题是它只渲染一个部分,最后一个。我错过了什么吗?这可以以更好的方式完成吗?

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 回答