0
0
Ruby on Railsframework~10 mins

API versioning in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the API version in the controller.

Ruby on Rails
module Api
  module V1
    class UsersController < ApplicationController
      def index
        render json: User.all, status: [1]
      end
    end
  end
end
Drag options to blanks, or click blank then click option'
A200
B201
C404
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Using 201 which means resource created, not appropriate for index action.
Using 404 which means not found.
Using 500 which means server error.
2fill in blank
medium

Complete the route to scope API versioning under /api/v1.

Ruby on Rails
Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :users, only: [:index, :show] [1]
    end
  end
end
Drag options to blanks, or click blank then click option'
Ado
Bscope
Cblock
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'end' which closes a block instead of opening one.
Using 'scope' which is a different routing method.
Using 'block' which is not a Ruby keyword.
3fill in blank
hard

Fix the error in the versioning header check in the controller.

Ruby on Rails
class Api::V2::ProductsController < ApplicationController
  before_action :check_version

  private

  def check_version
    unless request.headers['Accept'] == 'application/vnd.myapp.v[1]+json'
      render json: { error: 'Unsupported API version' }, status: 400
    end
  end
end
Drag options to blanks, or click blank then click option'
A2
B1
Cv2
Dv1
Attempts:
3 left
💡 Hint
Common Mistakes
Including 'v' in the version number causing mismatch.
Using the wrong version number.
Not matching the header string exactly.
4fill in blank
hard

Fill both blanks to define a versioned API module and controller class.

Ruby on Rails
module Api
  module [1]
    class [2]Controller < ApplicationController
      def show
        render json: { message: 'Versioned API' }
      end
    end
  end
end
Drag options to blanks, or click blank then click option'
AV3
BUsers
CProducts
DV1
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing version and resource names.
Using lowercase module names.
Using incorrect controller names.
5fill in blank
hard

Fill all three blanks to create a versioned API route with a custom constraint.

Ruby on Rails
Rails.application.routes.draw do
  namespace :api, defaults: { format: :json } do
    namespace :[1], constraints: { subdomain: '[2]' } do
      resources :[3]
    end
  end
end
Drag options to blanks, or click blank then click option'
Av1
Bapi
Corders
Dv2
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong version namespace.
Using incorrect subdomain string.
Using wrong resource name.