0
0
Ruby on Railsframework~20 mins

JSON rendering in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Rendering Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the JSON output of this Rails controller action?
Consider this Rails controller action rendering JSON. What will be the exact JSON output sent to the client?
Ruby on Rails
def show
  user = User.new(id: 1, name: "Alice", admin: false)
  render json: { id: user.id, name: user.name }
end
A{"user":{"id":1,"name":"Alice"}}
B{"id":1,"name":"Alice","admin":false}
C{"id":1,"name":"Alice"}
D[{"id":1,"name":"Alice"}]
Attempts:
2 left
💡 Hint
Look at the hash passed to render json: and what keys it contains.
📝 Syntax
intermediate
2:00remaining
Which option correctly renders a JSON array of user names in Rails?
You want to render JSON with an array of user names from a list of User objects. Which code snippet produces the correct JSON output?
Ruby on Rails
users = [User.new(name: "Alice"), User.new(name: "Bob")]
render json: ???
Ausers.map { |u| {name: u.name} }
Busers.pluck(:name)
Cusers.to_json(:only => [:name])
Dusers.map(&:name)
Attempts:
2 left
💡 Hint
You want a simple array of strings, not an array of hashes.
🔧 Debug
advanced
2:00remaining
Why does this Rails JSON render raise an error?
This code raises an error when rendering JSON. What is the cause?
Ruby on Rails
def index
  users = User.all
  render json: { users: users }
end
AActiveRecord objects cannot be directly converted to JSON without serialization
BThe hash keys must be strings, not symbols
CThe users variable is nil causing a NoMethodError
DThe render method requires a template file for JSON
Attempts:
2 left
💡 Hint
Think about how ActiveRecord objects convert to JSON by default.
state_output
advanced
2:00remaining
What JSON output results from this Rails view with Jbuilder?
Given this Jbuilder template, what JSON will be rendered?
Ruby on Rails
json.array! @users do |user|
  json.id user.id
  json.name user.name
end
A{"users":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}
B[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]
C[{"user":{"id":1,"name":"Alice"}},{"user":{"id":2,"name":"Bob"}}]
D{"array":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}
Attempts:
2 left
💡 Hint
array! creates a JSON array directly from the block.
🧠 Conceptual
expert
3:00remaining
Which option best explains how Rails handles JSON rendering with ActiveModel::Serializers?
When using ActiveModel::Serializers in Rails, how does the JSON rendering process work?
AThe serializer defines attributes and associations to include, and Rails uses it to build JSON instead of default to_json
BRails automatically converts all model attributes to JSON without any customization
CActiveModel::Serializers requires manual JSON string construction in the controller
DSerializers replace the controller render method and output HTML instead of JSON
Attempts:
2 left
💡 Hint
Think about how serializers customize JSON output.