I have a jbuilder template which conditionally renders a partial.
json.current_user do
# ...
json.avatar_urls do
json.partial! 'api/users/avatar_urls', avatar: user.avatar if user.avatar
end
end
When there is an avatar, the resulting JSON looks like (simplified):
"current_user": {
"avatar_urls": {
"original": "http://example.com/avatars/example.jpg",
}
}
But, when there is no avatar, the resulting JSON has no "avatar_urls" object at all:
"current_user": {
}
I want it to always have this object, but I want it to be an empty object:
"current_user": {
"avatar_urls": {
}
}
How can I achieve this?