Bird
Raised Fist0
Ruby on Railsframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does render json: @user do in a Rails controller?
easy
A. It sends the @user object as a JSON response to the client.
B. It renders an HTML view named 'json'.
C. It saves the @user object to the database in JSON format.
D. It redirects the user to a JSON page.

Solution

  1. Step 1: Understand the render method with json option

    The render json: command in Rails sends data as JSON format to the client instead of rendering an HTML view.
  2. Step 2: Identify the effect of @user in this context

    Passing @user sends the user object data as JSON in the HTTP response.
  3. Final Answer:

    It sends the @user object as a JSON response to the client. -> Option A
  4. Quick Check:

    render json: @user = send JSON response [OK]
Hint: render json: sends data as JSON response [OK]
Common Mistakes:
  • Thinking it renders an HTML view named 'json'
  • Confusing render json with saving data
  • Assuming it redirects to another page
2. Which of the following is the correct syntax to render a JSON response with a status code 201 in Rails?
easy
A. render json: @post, code: 201
B. render json => @post, status = 201
C. render json: @post status 201
D. render json: @post, status: 201

Solution

  1. Step 1: Identify correct Ruby hash syntax for render options

    In Rails, options are passed as key: value pairs separated by commas, e.g., json: @post, status: 201.
  2. Step 2: Check each option for syntax correctness

    render json: @post, status: 201 uses correct Ruby hash syntax. Options B, C, and D have syntax errors or wrong keys.
  3. Final Answer:

    render json: @post, status: 201 -> Option D
  4. Quick Check:

    Correct Ruby hash syntax = render json: @post, status: 201 [OK]
Hint: Use commas and colons for options in render [OK]
Common Mistakes:
  • Using => instead of : in Ruby 3+ syntax
  • Missing commas between options
  • Using wrong keys like code instead of status
3. Given the controller code:
def show
  @user = User.find(1)
  render json: @user.to_json(only: [:id, :name])
end

What will the JSON response include?
medium
A. {"id":1,"name":"Alice"}
B. {"name":"Alice"}
C. {"id":1}
D. {"id":1,"name":"Alice","email":"alice@example.com"}

Solution

  1. Step 1: Understand the use of to_json(only: [:id, :name])

    This limits the JSON output to only the id and name fields of the user object.
  2. Step 2: Identify which fields appear in the JSON response

    Only id and name are included, so email is excluded.
  3. Final Answer:

    {"id":1,"name":"Alice"} -> Option A
  4. Quick Check:

    only: [:id, :name] = id and name fields only [OK]
Hint: only: limits JSON fields included [OK]
Common Mistakes:
  • Assuming all fields are included by default
  • Confusing only: with except:
  • Not calling to_json with options
4. What is wrong with this Rails controller code?
def index
  @users = User.all
  render json: @users only: [:id, :email]
end
medium
A. The @users variable should be a single user, not all users.
B. The only option should be inside to_json method, not passed directly to render.
C. You cannot render collections as JSON in Rails.
D. The render method requires a template name.

Solution

  1. Step 1: Check how to limit JSON fields for collections

    The only option must be passed inside to_json or as a render json: option hash, not as a separate argument.
  2. Step 2: Identify the correct way to pass only option

    Correct usage: render json: @users.to_json(only: [:id, :email]) or render json: @users, only: [:id, :email] inside a hash, but the given code passes only outside the hash.
  3. Final Answer:

    The only option should be inside to_json method, not passed directly to render. -> Option B
  4. Quick Check:

    only option must be inside to_json or render json hash [OK]
Hint: Pass only inside to_json or render json hash [OK]
Common Mistakes:
  • Passing only as separate argument outside render hash
  • Thinking collections can't be rendered as JSON
  • Forgetting to call to_json with options
5. You want to render a JSON response with a list of users, but only include users with active status true and only their id and name fields. Which code correctly does this in a Rails controller?
hard
A. render json: User.where(active: true) only: [:id, :name]
B. render json: User.all.select(:id, :name).where(active true)
C. render json: User.where(active: true).to_json(only: [:id, :name])
D. render json: User.where(active: true).only(:id, :name)

Solution

  1. Step 1: Filter users by active status

    Use User.where(active: true) to get only active users.
  2. Step 2: Limit JSON fields to id and name

    Call to_json(only: [:id, :name]) to include only those fields in the JSON output.
  3. Step 3: Check each option for correctness

    render json: User.where(active: true).to_json(only: [:id, :name]) correctly chains filtering and JSON field limiting. render json: User.all.select(:id, :name).where(active: true) has wrong method order and will error. render json: User.where(active: true), only: [:id, :name] passes only incorrectly. render json: User.where(active: true).only(:id, :name) uses a non-existent only method on ActiveRecord relation.
  4. Final Answer:

    render json: User.where(active: true).to_json(only: [:id, :name]) -> Option C
  5. Quick Check:

    Filter then to_json with only: fields [OK]
Hint: Filter first, then call to_json with only: [OK]
Common Mistakes:
  • Using only as a method on ActiveRecord relation
  • Passing only outside to_json or render json hash
  • Selecting fields before filtering causing errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard