Show Users

Add the standard HTTP method, GET, for the User model in the API.

Define the show action

We will define the show action by simply adding the action to our controllers in app/controllers/api/v1/users_controller.rb. The code for the show function is as follows:

Press + to interact
class Api::V1::UsersController < ApplicationController
# GET /users/1
def show
render json: User.find(params[:id])
end
end

Add test

There are two things we want to test for an API:

  • The JSON structure returned by the server.
  • The HTTP response code returned by the server.

Common HTTP codes

The first digit of the status code specifies one of the five response classes. The bare minimum for an HTTP client is that it uses one of these five classes. Here is a list of commonly used HTTP codes. ...