[英]Rails - Jbuilder and use of partials?
I have a template that takes an array of objects, and for each object it needs to render a partial template. 我有一个采用对象数组的模板,并且对于每个对象,它都需要呈现一个局部模板。 The result comes back as empty objects. 结果作为空对象返回。 Any idea what's wrong in the code?? 知道代码中有什么问题吗?
Controller 调节器
def index
render text: render_to_string(template: 'users.json.jbuilder', locals: {:@uses : users}
end
users.json.jbuilder users.json.jbuilder
json.array! @users do |user|
json.partial! 'user.json.jbuilder', { user: user}
end
user.json.jbuilder user.json.jbuilder
Jbuilder.encode do |json|
json.id user.id
json.username user.user_name
end
Result 结果
[{},{},{},{},{},{},{},{},{},{},{},{},{},{}]
user.json.jbuilder
means that Jbuilder.encode do |json|
user.json.jbuilder
意味着Jbuilder.encode do |json|
is already summoned. 已经被召唤了。
So, your user.json.jbuilder
file would become functional as follows: 因此,您的user.json.jbuilder
文件将具有以下功能:
json.id user.id
json.username user.user_name
That's it... End of story :) 就这样...故事的结尾:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.