简体   繁体   English

无法使用jbuilder渲染多个局部

[英]unable to render more than one partial with jbuilder

I have created a controllers function to get all elements of three different models. 我创建了一个控制器函数来获取三个不同模型的所有元素。 As simple as this: 就这么简单:

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

Then in get_all_data.json: 然后在get_all_data.json中:

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

The problem is it only renders one partial, the last one. 问题在于它只渲染一个局部,最后一个。 Am i missing something? 我想念什么吗? Could this be done in some better way? 可以用更好的方法做到吗?

Try putting them each in their own members of the JSON structure: 尝试将它们分别放入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

I feel like that should work. 我觉得那应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM