Ruby On Rails RESTful API

Steve Carter
2 min readJan 21, 2022

What is REST?

Representational State Transfer is an architectural pattern. At it’s core, it defines a set of constraints for building robust web apps allowing for more flexibility and scalability.

The Fielding Constraints

There are many architectural constraints that a system must satisfy to be considered RESTful.

Client-Server Constraint

must be made up of clients and servers. A server is a computer that has resources of interest, and a client is a computer that wants to interact with the resources stored on the server. When you browse the Internet, your computer is acting as a client and sends HTTP requests to a server in order to access and manipulate information. A RESTful system has to operate in the client-server model, even if a component sometimes acts like a client and sometimes acts like a server.

Stateless

This simply means they do not need to keep track of each others state. When a client is not interacting with the server, the server has no idea of its existence. The server also does not keep a record of past requests. Each request is treated as a standalone.

Identification of Resources

For any given entity within our application, we should be able to access that entity at a stable identifier, a URL typically. this constraint is about determining what the entities or “resources” your application exposes, and then mapping to them stable URLs. Rails handle most of this for us. you should lean into Rails’ REST foundation by making use of resources and resource for defining our routes, and ensuring we have proper routing.

Manipulation through representations

This constraint breaks down into two pieces, the manipulation aspect, and the representations. The manipulation here to using the built-in HTTP verbs like GET, POST, PUT, DELETEto manipulate our resources, rather than encoding these actions in the URL. representations deal’s with the idea that we may want to see an HTML page for a given resource, or we may want to see a JSON response.

Hypermedia and HATEOS

Hypermedia is really just saying that we embed links within our documents. Further, HATEOAS implies that these links are the mechanism for exploring and interacting with / manipulating the resources within our application. It turns out that while this is common for HTML web applications, it is generally considered to be less useful within an API.

REST in Rails

Rails provides us with a great foundation for building RESTful web applications, but it’s very easy to introduce non-restful actions into our application and fall into a slippery slope of non-RESTfulness.

--

--