Git Product home page Git Product logo

phase-4-rails-resources-destroy's Introduction

Rails Resource Routing: Destroy

Learning Goals

  • Delete a resource using Rails

Introduction

In this lesson, we'll continue working on our Bird API by adding a destroy action, so that clients can use our API to delete birds. To get set up, run:

$ bundle install
$ rails db:migrate db:seed

This will download all the dependencies for our app and set up the database.

HTTP Verb Path Controller#Action Description
GET /birds birds#index Show all birds
POST /birds birds#create Create a new bird
GET /birds/:id birds#show Show a specific bird
PATCH or PUT /birds/:id birds#update Update a specific bird
DELETE /birds/:id birds#destroy Delete a specific bird

Deleting a Bird

We're at the last step on our journey to becoming CRUD experts! Our goal is to give users the ability to delete birds via the API. To start, we'll need to set up a route to handle a DELETE /birds/:id request. We can do so by adding :destroy to our resources:

resources :birds, only: [:index, :show, :create, :update, :destroy]

And since we're now using all five RESTful routes, we can omit the only option:

resources :birds

Running rails routes will show us all the RESTful routes in our application, plus our custom route:

$ rails routes
Prefix Verb   URI Pattern               Controller#Action
 birds GET    /birds(.:format)          birds#index
       POST   /birds(.:format)          birds#create
  bird GET    /birds/:id(.:format)      birds#show
       PATCH  /birds/:id(.:format)      birds#update
       PUT    /birds/:id(.:format)      birds#update
       DELETE /birds/:id(.:format)      birds#destroy
       PATCH  /birds/:id/like(.:format) birds#increment_likes

We'll also need to add a destroy action to our controller where we'll be deleting the bird from the database:

def destroy
  bird = Bird.find_by(id: params[:id])
  if bird
    bird.destroy
    head :no_content
  else
    render json: { error: "Bird not found" }, status: :not_found
  end
end

In this controller action, our goal is to:

  • Find a bird using the ID from the route params
  • Remove it from the database with bird.destroy

You'll also notice that instead of rendering a JSON response, we're returning head :no_content if our bird was successfully deleted. :no_content will give a 204 status code, indicating that the server has successfully fulfilled the request and that there is no content to send in the response. We're also not sending any payload of data in the body of the request.

One thing to watch out for: if the API doesn't return JSON data, and you try to read the response data from a fetch request, you will get an error:

fetch("http://localhost:3000/birds/3", {
  method: "DELETE",
})
  .then((r) => r.json()) // this line will error out, because there is no JSON to parse!
  .then((data) => console.log(data));

Depending on your needs, you could send back a JSON response to verify that the request was completed successfully. For example, json-server handles a successful delete request by sending an empty object:

bird.destroy
render json: {}

Ultimately, you can decide which option you prefer based on how you'll use this data in your client application.

Make sure to test these out in Postman to see the difference between using head and render.

Conclusion

Now that we've covered the Delete action, you can perform all four CRUD actions with Rails and do so following RESTful conventions!

Check For Understanding

Before you move on, make sure you can answer the following question:

  1. What options did you learn about in this lesson for returning information to your users about the status of a delete request?

Resources

phase-4-rails-resources-destroy's People

Contributors

ihollander avatar lizbur10 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.