DEV Community

Thadeus Nyariki
Thadeus Nyariki

Posted on

REST and RESTful convention in Ruby on Rails

Representational State Transfer (REST) is a software architecture that is commonly used to build web applications. REST is a simple, lightweight and scalable way to create APIs that are used to exchange data between different systems over HTTP. RESTful APIs are built on top of the HTTP protocol, using its standard methods (GET, POST, PUT, DELETE) to interact with resources.

Ruby on Rails and REST

Ruby on Rails is a popular web development framework that follows the RESTful conventions by default. In Rails, RESTful routes are defined in a simple and intuitive way using the resources method in the routes file. The resources method generates a set of RESTful routes for a given controller, including the seven standard actions (index, show, new, create, edit, update, destroy).

For example, suppose we have a controller called PostsController. We can define the RESTful routes for this controller using the following code:

resources :posts

This will generate the following routes:

GET    /posts             posts#index
GET    /posts/new         posts#new
POST   /posts             posts#create
GET    /posts/:id         posts#show
GET    /posts/:id/edit    posts#edit
PUT    /posts/:id         posts#update
DELETE /posts/:id         posts#destroy
Enter fullscreen mode Exit fullscreen mode

RESTful conventions in Rails Controllers

In Rails, RESTful controllers should follow the following conventions:

The controller name should be pluralized (e.g. PostsController instead of PostController).
The controller should inherit from ApplicationController.
The controller should define the standard RESTful actions (index, show, new, create, edit, update, destroy).
The controller should load the resource(s) from the database in a before_action callback.
The controller should respond to different formats (HTML, JSON, XML) using the respond_to method.
Here is an example of a simple PostsController that follows these conventions:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  def index
    @posts = Post.all
  end

  def show
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)

    if @post.save
      redirect_to @post
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @post.update(post_params)
      redirect_to @post
    else
      render :edit
    end
  end

  def destroy
    @post.destroy
    redirect_to posts_url
  end

  private

  def set_post
    @post = Post.find(params[:id])
  end

  def post_params
    params.require(:post).permit(:title, :body)
  end
end
Enter fullscreen mode Exit fullscreen mode

In this example, the before_action callback set_post loads the resource from the database before each action that needs it (show, edit, update, destroy).

Conclusion

In summary, Ruby on Rails provides a simple and intuitive way to build RESTful APIs using the resources method in the routes file. RESTful conventions in Rails controllers follow a set of best practices that make it easy to create maintainable and scalable APIs. By following these conventions, Rails developers can create RESTful APIs that are easy to use and understand.

Top comments (0)