DEV Community

Cover image for CRUD with Rails
KevinZ-CS
KevinZ-CS

Posted on

CRUD with Rails

In this blog we will be going over CRUD features in rails. The first question you may ask is what is CRUD? CRUD is an acronym that stands for create, read, update, and delete. With these actions a user can interact with a database in our rails backend and send over a response to the frontend.

Read

To start off, we will be covering the read action from CRUD. This action is actually split into two separate actions, index and show. Both are similar in the sense that they respond to a GET request from the frontend. The main difference is that index responds with a collection of data while show responds with a specific piece of data.

To get this properly set up with rails in the backend you will need to first set up routes with actions assigned to it. This should look something like this:

Rails.application.routes.draw do
  get '/games', to: 'games#index'
  get '/games/:id', to: 'games#show'
end
Enter fullscreen mode Exit fullscreen mode

As you can see this route receives a get request from the frontend at the games route. The second half of the route addresses the controller and action that will be handling that request. So in our example the games controller will be handling our get request using the index and show action.

One thing you may notice is the :id that is part of the second route with the show action. Remember, as previously mentioned the show action unlike index will only respond with a single specific piece of data. That id is meant to identify that specific data and is included in the url of the fetch request from the frontend. From that url that id will be captured in an object called params at the backend and this will be used in the controller as you will soon see.

So in the games controller you would see something like this:

class GamesController < ApplicationController

  # GET /games
  def index
    games = Games.all
    render json: games
  end

  # GET /games/:id
  def show
    game = Game.find_by(id: params[:id])
    render json: game
  end

end
Enter fullscreen mode Exit fullscreen mode

As you can see the index action will grab all games from the database and store it in the games variable and send a response that contains all the games data in json format.

In the show action notice how we are looking for a specific piece of data based on the id attribute using the value (obtained from the url of the fetch request) thats stored in the params object. Again that data will be stored in a variable and sent to the frontend as a response in json format.

Create

Next, we will discuss the create action. The create action uses data from the frontend via params object to create new data and then save it to the database. Like before, to set this up we will need to add a route associated with the create action. So in our routes file it should look something like this:

Rails.application.routes.draw do
  post '/games', to: 'games#create'
end
Enter fullscreen mode Exit fullscreen mode

As you can see, this route receives a post request from the frontend at the games route which will use the create action in the games controller to handle the request. In the games controller it should look something like this:

class GamesController < ApplicationController

  # POST /game
  def create
  game = Game.create(name: params[:name], price: 
  params[:price])
  render json: game, status: :created
  end

end
Enter fullscreen mode Exit fullscreen mode

Notice how we're using the params object again in this action. In order to create and save data in our database in our backend we need information on what to create. This information comes from the frontend request. In a post request we include data in the body of the request. In this case the body of our request would include the name and price of the game. Once that request has been received at the backend, data from the body of the request is then saved into the params object which can then be used in our create method to create and save new data into our database.

Update

Next, we have the update action. The update action is used (as the name suggests) to update existing data in our database. Like the other actions we will need to create a route that's associated with this action. In our routes file it should look something like this:

Rails.application.routes.draw do
  patch '/games/:id', to: 'games#update'
end
Enter fullscreen mode Exit fullscreen mode

This route receives a patch request from the frontend at a route that contains the id of the data that we want to update and the request will be handled with an update action in the games controller. The games controller should look something like this:

class GamesController < ApplicationController

  # PATCH /game
  def update
  game = Game.find_by(id: params[:id])
  game = Game.update(name: params[:name], price: 
  params[:price])
  render json: game
  end
end
Enter fullscreen mode Exit fullscreen mode

In this example we are using the params object again but this time it contains more data than before. The params contain the id from the url as well as the updated data in the body of the patch request sent from the frontend. With that in mind, the update action will use the id from the params object to find the specific data that will be updated and updates it with the new data sent from the body of the patch request. Finally, the updated game will be sent back to the frontend in json format.

Delete

Lastly, we have the delete request which uses the destroy action in the backend. We set up our route for this action like so:

Rails.application.routes.draw do
  delete '/games/:id', to: 'games#destroy'
end
Enter fullscreen mode Exit fullscreen mode

Delete requests from the frontend will be sent to this route and handled with the destroy action in the games controller. We have an id in the route because we are expecting to delete a specific data with that id which should be present in the fetch request url. In the games controller it should look something like this:

class GamesController < ApplicationController

  # DELETE /games/:id
  def destroy
    game = Game.find_by(id: params[:id])
    head :no_content
  end
end
Enter fullscreen mode Exit fullscreen mode

In this action we are simply finding a specific data with the id stored in the params object and then deleting it from the database.

Conclusion

In summary, from the backend perspective you will need a routes file to perform CRUD. In the routes file each route will need a http verb (type of request: get, patch, delete etc.), the route, as well as the controller and action that will be handling the request. You will also need to make sure that the actions in the controller file is handling the request correctly and returning the appropriate data. Hope this helps!

Top comments (0)