optional fields for jbuilder templates
Add jbuilder_optionals
to your Gemfile and bundle install
:
gem 'jbuilder_optionals'
after using this gem you can add optional fields on jbuilder template and when you want render tell template what fields you want to use in render
#app/views/partials/_user.jbuilder
json.id user.id
json.optional! :username, user.username
json.optional! :phone, user.phone
json.partial! 'partials/user', user: @user
#will render: {id: 1}
now only you need specify what fields contains
json.partial! 'partials/user', user: @user, contains: [:username]
#will render: {id: 1, username: 'wise_moe'}
also you can use partials as a optional field in another partial
#app/views/partials/_pet.jbuilder
json.id pet.id
json.optional! :name, pet.name
#app/views/partials/_user.jbuilder
json.id user.id
json.optional! :username, user.username
json.optional! :phone, user.phone
json.optional_partial! :pet, user.pet
json.partial! 'partials/user', user: @user, contains: [:username, :pet]
# will render: {id: 1, username: 'wise_moe', pet: {id: 1}}
json.partial! 'partials/user', user: @user, contains: [:username, pet: [:name]]
# will render: {id: 1, username: 'wise_moe', pet: {id: 1, name: 'canary'}}
if you have many pets all of them will be render
#app/views/partials/_user.jbuilder
json.id user.id
json.optional! :username, user.username
json.optional! :phone, user.phone
json.optional_partial! :pets, user.pets
json.partial! 'partials/user', user: @user, contains: [:username, :pets]
# will render: {id: 1, username: 'wise_moe', pets: [{id: 1}, {id: 2}]}
json.partial! 'partials/user', user: @user, contains: [:username, pets: [:name]]
# will render: {id: 1, username: 'wise_moe', pets: [{id: 1, name: 'canary'}, {id: 2, name: 'giraffe'}]}