Skip to content

Instantly share code, notes, and snippets.

@pbonneville
Last active June 3, 2016 16:33
Show Gist options
  • Save pbonneville/f8226a46a927be80e6044f440a5ef8a2 to your computer and use it in GitHub Desktop.
Save pbonneville/f8226a46a927be80e6044f440a5ef8a2 to your computer and use it in GitHub Desktop.
Sample of Rails jbuilder template to create customized JSON output. This also shows how to access related object values and how to iterate over a has_many relationship in order to get to nested values. This is verified to work in Rail 4+ and 5rc1.
# app/views/controller_name/index.json.jbuilder
json.array!(@pose_blocks) do |pose_block|
if pose_block.published == true
json.extract! pose_block, :unique_id, :name, :pose_id_for_cover_image, :video_duration, :video_size, :duration
json.focus_type pose_block.focus_type.name
json.ability_type pose_block.ability_type.name
json.poses pose_block.pose_block_poses.each do |pose_block_pose|
json.unique_id pose_block_pose.pose.unique_id
json.duration pose_block_pose.duration
json.sort_order pose_block_pose.sort_order
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment