Giter VIP home page Giter VIP logo

phase-4-rails-resources-index-show's Introduction

Rails Resource Routing: Index and Show

Learning Goals

  • Explain the benefits of RESTful routing conventions in Rails
  • Use resources to generate routes
  • Understand how to view all routes in a Rails application

Introduction

In this module, we'll be building out full CRUD with the five RESTful routes for one resource. We'll be making an API for birding enthusiasts, so the resource we'll be working with is birds. In this lesson, we'll cover the two routes for read actions to display information about the birds in our database: the index and show actions.

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

Setup

First, we'll need to install the dependencies as usual:

$ bundle install

Next, we'll need to make a Bird model and generate some sample data for our API. Let's use a Rails generator to set up our model:

$ rails g model Bird name species --no-test-framework

After generating the model, run the migration:

$ rails db:migrate

Next, we'll create some seed data in the db/seeds.rb file:

Bird.create!(name: "Black-Capped Chickadee", species: "Poecile Atricapillus")
Bird.create!(name: "Grackle", species: "Quiscalus Quiscula")
Bird.create!(name: "Common Starling", species: "Sturnus Vulgaris")
Bird.create!(name: "Mourning Dove", species: "Zenaida Macroura")

To add these birds to our database, run the seed file:

$ rails db:seed

Creating RESTful Routes with Resources

To set up our index and show actions, we'll first need to create some routes. Recall that the RESTful convention for these routes is as follows:

GET /birds      => show a list of all birds
GET /birds/:id  => show one specific bird

Just like in previous lessons, we can build the routes for these actions in Rails by hand, like so:

# config/routes.rb

Rails.application.routes.draw do
  get '/birds', to: 'birds#index'
  get '/birds/:id', to: 'birds#show'
end

We can verify that these routes were added successfully by viewing all of our app's routes using a handy Rake task. In your terminal, run:

$ rails routes
Prefix  Verb  URI Pattern           Controller#Action
 birds  GET   /birds(.:format)      birds#index
        GET   /birds/:id(.:format)  birds#show

However, there is a better way to build the routes: Rails provides a tool, resources, that enforces RESTful conventions and saves us work.

Edit the config/routes.rb file like so:

Rails.application.routes.draw do
  resources :birds
end

Then, run rails routes again:

$ 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

With just one line of code โ€” resources :birds โ€” Rails created all the RESTful routes we need and mapped them to the appropriate controller action!

With great power comes great responsibility. Even though we'll eventually add all of these RESTful routes to our API, for the time being, we only need two: the index and show routes. We can still use resources and customize which routes are created like so:

Rails.application.routes.draw do
  resources :birds, only: [:index, :show]
end

Running rails routes now will give us the same output as when we wrote out the routes by hand:

$ rails routes
Prefix  Verb  URI Pattern           Controller#Action
 birds  GET   /birds(.:format)      birds#index
        GET   /birds/:id(.:format)  birds#show

As a rule, you should only generate routes that your API is actually using. If you create a route using resources, but don't implement the controller action for that route, your API's consumers (any clients using your API) will get an unexpected response if they try to use a route that doesn't exist.

Handling the Index and Show Actions

To complete our first couple RESTful actions, let's set up a controller:

$ rails g controller Birds --no-test-framework

In the controller, add the following code:

# app/controllers/bird_controller.rb

class BirdsController < ApplicationController

  # GET /birds
  def index
    birds = Bird.all
    render json: birds
  end

  # GET /birds/:id
  def show
    bird = Bird.find_by(id: params[:id])
    if bird
      render json: bird
    else
      render json: { error: "Bird not found" }, status: :not_found
    end
  end

end

Run rails s and visit the /birds and /birds/1 endpoints in the browser to check your work!

Conclusion

In this lesson, we learned how to set up the two "Read" CRUD actions. Specifically, we set up the routes to handle the requests as well as the controller actions to return the requested information. We also learned how to use a built-in tool, resources, as an easier way to set up routes that follow REST conventions.

Check For Understanding

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

  1. Tools like resources are examples of Rails' use of the convention over configuration paradigm. What are the advantages of this approach?
  2. Why is it a good idea to limit the routes you create to ones that your API will actually use?

Resources

phase-4-rails-resources-index-show's People

Contributors

alveem avatar ihollander avatar lizbur10 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phase-4-rails-resources-index-show's Issues

Test cases not completed

Canvas Link

https://learning.flatironschool.com/courses/4995/assignments/173236?module_item_id=377598

Concern

This lab is for implementing an index and show route. The index test is incomplete and there isn't one for show. This is the test file:

require 'rails_helper'

RSpec.describe "Birds", type: :request do
describe "GET /index" do
pending "add some examples (or delete) #{FILE}"
end
end

Additional Context

No response

Suggested Changes

Write the test cases or remove the test file.

no bundle install direction

Thanks for raising this issue! Future learners thank you for your diligence. In
order to help the curriculum team address the problem, please use this template
to submit your feedback. We'll work on addressing the issue as soon as we can.

Please fill out as much of the information below as you can (it's ok if you
don't fill out every section). The more context we have, the easier it will be
to fix your issue!

Note: you should only raise issues related to the contents of this lesson.
If you have questions about your code or need help troubleshooting, reach out to
an instructor/your peers.


Link to Canvas

https://learning.flatironschool.com/courses/3498/assignments/128238?module_item_id=264823

What should be changed?

The lab is missing the direction to run bundle install.

Additional context

I know that most should know to do this, but all other labs have included this direction, so it might be nice to add!

Unable to fork

Thanks for raising this issue! Future learners thank you for your diligence. In
order to help the curriculum team address the problem, please use this template
to submit your feedback. We'll work on addressing the issue as soon as we can.

Please fill out as much of the information below as you can (it's ok if you
don't fill out every section). The more context we have, the easier it will be
to fix your issue!

Note: you should only raise issues related to the contents of this lesson.
If you have questions about your code or need help troubleshooting, reach out to
an instructor/your peers.


Link to Canvas

https://learning.flatironschool.com/courses/4207/assignments/135826?module_item_id=281617
Add a link to the assignment in Canvas here.

Describe the bug

I was not able to fork the lesson using the blue FORK button nor clicking the octocat logo and clicking Fork in the top left.
Write a clear and concise description of what the bug is. Include the file and
line number(s) if possible.

What is the expected behavior?

Write a clear and concise description of what you expected to happen.

Screenshots

If applicable, attach screenshots to help explain your problem.

What OS are you using?

  • OS X

Any additional context?

Add any other context about the problem here.

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.