Complete the code to define the API version in the controller.
module Api
module V1
class UsersController < ApplicationController
def index
render json: User.all, status: [1]
end
end
end
endThe HTTP status code 200 means the request was successful and the server is returning the requested data.
Complete the route to scope API versioning under /api/v1.
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users, only: [:index, :show] [1]
end
end
endThe do keyword starts a block for the resources method to define nested routes or options.
Fix the error in the versioning header check in the controller.
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
The header should match the version number without the 'v' prefix because the string already includes 'v'.
Fill both blanks to define a versioned API module and controller class.
module Api module [1] class [2]Controller < ApplicationController def show render json: { message: 'Versioned API' } end end end end
The module name should be the version like V3 and the controller name should be the resource like Products.
Fill all three blanks to create a versioned API route with a custom constraint.
Rails.application.routes.draw do
namespace :api, defaults: { format: :json } do
namespace :[1], constraints: { subdomain: '[2]' } do
resources :[3]
end
end
endThe version namespace is v2, the subdomain constraint is api, and the resource is orders.