简体   繁体   中英

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

json.array! @users do |user|
     json.partial! 'user.json.jbuilder', { user: user}
end 

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| is already summoned.

So, your user.json.jbuilder file would become functional as follows:

json.id user.id
json.username user.user_name

That's it... End of story :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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