0
0
Ruby on Railsframework~20 mins

API versioning in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Versioning Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Rails route to different API versions?
Given a Rails API with versioning using namespace, what will be the response when calling GET /api/v2/products if only v1 namespace has a ProductsController?
Ruby on Rails
Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :products, only: [:index]
    end
    namespace :v2 do
      # No products controller defined here
    end
  end
end
A404 Not Found error because v2/products route is not defined
BReturns the product list from v1/products controller
CRedirects automatically to v1/products endpoint
D500 Internal Server Error due to missing controller
Attempts:
2 left
💡 Hint
Think about how Rails matches routes and controllers inside namespaces.
📝 Syntax
intermediate
2:00remaining
Correct syntax for versioning with Accept header in Rails
Which option correctly configures Rails to use API versioning via the Accept header with version 'v1'?
A
class Api::BaseController < ActionController::API
  before_action :check_version

  private
  def check_version
    render status: 406 unless request.headers['Accept'] == 'application/json; version=1'
  end
end
B
class Api::BaseController < ActionController::API
  before_action :check_version

  private
  def check_version
    render status: 406 unless request.headers['Accept']&.include?('application/vnd.myapp.v1+json')
  end
end
C
class Api::BaseController < ActionController::API
  before_action :check_version

  private
  def check_version
    render status: 406 unless request.headers['Content-Type'] == 'application/vnd.myapp.v1+json'
  end
end
D
class Api::BaseController < ActionController::API
  before_action :check_version

  private
  def check_version
    render status: 406 unless request.headers['Accept-Version'] == 'v1'
  end
end
Attempts:
2 left
💡 Hint
The Accept header is used to specify the media type and version.
state_output
advanced
2:00remaining
What is the response when version is missing in header?
In a Rails API that requires versioning via Accept header, what response code will the client get if the Accept header is missing?
Ruby on Rails
class Api::BaseController < ActionController::API
  before_action :check_version

  private
  def check_version
    unless request.headers['Accept']&.include?('application/vnd.myapp.v1+json')
      render json: { error: 'API version missing or unsupported' }, status: :not_acceptable
    end
  end
end
A406 Not Acceptable with error message
B200 OK with default version response
C400 Bad Request with no message
D500 Internal Server Error due to missing header
Attempts:
2 left
💡 Hint
Check the status code used in the render call.
🔧 Debug
advanced
2:00remaining
Why does this versioned API controller not respond correctly?
Given this controller, why does the API always respond with 404 Not Found when requesting /api/v1/users?
Ruby on Rails
module Api
  module V1
    class UsersController < ApplicationController
      def index
        render json: { users: [] }
      end
    end
  end
end

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      get 'users', to: 'users#index'
    end
  end
end
AThe controller inherits from ApplicationController instead of ActionController::API
BThe routes use get instead of resources, causing routing failure
CThe controller is namespaced but the file is not placed in the correct folder
DThe render json syntax is incorrect and causes failure
Attempts:
2 left
💡 Hint
Rails expects controller files to be in folders matching their module namespaces.
🧠 Conceptual
expert
3:00remaining
What is the main advantage of using URL path versioning over header versioning in Rails APIs?
Consider two common API versioning strategies in Rails: using URL path segments (e.g., /api/v1/) and using custom Accept headers. What is the main advantage of URL path versioning?
AIt automatically updates clients to the latest API version without changes
BIt allows more flexible version negotiation based on media types
CIt reduces the number of routes needed in the Rails router
DIt is easier for clients and browsers to test and cache because the version is visible in the URL
Attempts:
2 left
💡 Hint
Think about how URLs are used in browsers and caching systems.