Bird
Raised Fist0
Ruby on Railsframework~20 mins

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

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

Practice

(1/5)
1. What is the main purpose of API versioning in a Rails application?
easy
A. To change the database schema automatically
B. To allow updates without breaking existing API users
C. To speed up database queries
D. To improve the UI design of the application

Solution

  1. Step 1: Understand API versioning concept

    API versioning allows developers to release new API features or changes without affecting users who rely on older versions.
  2. Step 2: Identify the main benefit

    This prevents breaking changes and keeps old clients working while new clients can use updated features.
  3. Final Answer:

    To allow updates without breaking existing API users -> Option B
  4. Quick Check:

    API versioning = safe updates [OK]
Hint: API versioning = safe updates without breaking old clients [OK]
Common Mistakes:
  • Thinking it speeds up database queries
  • Confusing with UI improvements
  • Assuming it changes database schema automatically
2. Which of the following is the correct way to define API versioning in Rails routes using namespaces?
easy
A. version :v1 do resources :users end
B. scope :v1 do resources :users end
C. namespace :v1 do resources :users end
D. api_version :v1 do resources :users end

Solution

  1. Step 1: Recall Rails routing syntax for versioning

    Rails uses the namespace method to group routes under a version folder like v1.
  2. Step 2: Check each option

    Only namespace :v1 do ... end is valid Rails syntax for versioning routes.
  3. Final Answer:

    namespace :v1 do resources :users end -> Option C
  4. Quick Check:

    Rails versioning uses namespace [OK]
Hint: Use namespace for version folders in routes [OK]
Common Mistakes:
  • Using non-existent 'version' or 'api_version' methods
  • Using scope without path or module options
  • Confusing namespace with scope
3. Given this routes setup in Rails:
namespace :v1 do
  get 'status', to: 'health#show'
end
namespace :v2 do
  get 'status', to: 'health#show'
end

What will GET /v2/status call?
medium
A. The show action in HealthController inside V2 module
B. The show action in the main HealthController without module
C. A routing error because v2 is not defined
D. The show action in HealthController inside V1 module

Solution

  1. Step 1: Understand namespace routing effect

    Each namespace creates a module scope. So v2 routes call controllers inside V2:: module.
  2. Step 2: Match the route to controller

    GET /v2/status calls V2::HealthController#show, not the v1 or main controller.
  3. Final Answer:

    The show action in HealthController inside V2 module -> Option A
  4. Quick Check:

    Namespace v2 routes call V2 controllers [OK]
Hint: Namespace URL matches controller module [OK]
Common Mistakes:
  • Assuming v2 calls v1 controller
  • Thinking it causes routing error
  • Ignoring module scoping in namespaces
4. You have this routes file snippet:
namespace :v1 do
  resources :products
end

namespace :v1 do
  resources :orders
end

What is the problem with this code?
medium
A. You must use scope instead of namespace for versioning
B. Rails does not allow multiple namespaces with the same name
C. Resources inside namespaces must be singular, not plural
D. Duplicate namespace declarations can be merged into one block

Solution

  1. Step 1: Analyze namespace usage

    Rails allows multiple namespace blocks with the same name but it's better style to merge them.
  2. Step 2: Identify best practice

    Merging namespace :v1 blocks into one improves readability and avoids confusion.
  3. Final Answer:

    Duplicate namespace declarations can be merged into one block -> Option D
  4. Quick Check:

    Merge same namespaces for clarity [OK]
Hint: Merge same namespaces into one block [OK]
Common Mistakes:
  • Thinking Rails forbids multiple same namespaces
  • Using singular resources incorrectly
  • Confusing namespace with scope
5. You want to support two API versions in your Rails app: v1 and v2. v1 returns user data with name only, but v2 adds email. How should you organize your controllers to handle this cleanly?
hard
A. Create separate controllers under app/controllers/v1/users_controller.rb and app/controllers/v2/users_controller.rb with different responses
B. Use a single controller and check version in each action to decide response
C. Put all logic in the model and use one controller for both versions
D. Use middleware to rewrite responses after controller runs

Solution

  1. Step 1: Understand version separation best practice

    Separate controllers per version keep code clean and maintainable, avoiding complex conditionals.
  2. Step 2: Apply Rails versioning pattern

    Place controllers in versioned folders like v1/users_controller.rb and v2/users_controller.rb to handle different responses.
  3. Final Answer:

    Create separate controllers under app/controllers/v1/users_controller.rb and app/controllers/v2/users_controller.rb with different responses -> Option A
  4. Quick Check:

    Separate controllers per version = clean code [OK]
Hint: Use separate controllers per version folder [OK]
Common Mistakes:
  • Using one controller with many version checks
  • Putting version logic in models
  • Trying to rewrite responses with middleware
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