Giter VIP home page Giter VIP logo

phase-4-rails-create-read-lab's Introduction

Rails Create, Index, and Show Lab

Learning Goals

  • Generate create, index and show routes for one resource
  • Use strong params to create a new resource

Introduction

In this lab, we'll be building an API for a plant store! In addition to our usual Rails code, there is code for a React frontend application in the client directory.

The code for the frontend application is done. Your job is to create the Rails API so that the fetch requests on the frontend work successfully.

Instructions

The React application is in the client directory. To set it up, from the root directory, run:

$ npm install --prefix client

Using --prefix client will run the npm command within the client directory.

To set up your backend, run:

$ bundle install

To see how the React application and Rails API are interacting, you can run both the Rails application and the React application together by running:

$ rails start

This will run a Rake task that will start both the Rails app and the React app. You must use rails start (not rails s) to start both applications together!

Take a look through the components in the client/src/components/ folder to get a feel for what our app does. Note that the fetch requests in the frontend (in NewPlantForm and PlantPage) don't include the backend domain:

fetch("/plants");
// instead of fetch("http://localhost:3000/plants")

This is because we are proxying these requests to our API.

Deliverables

Model

Create a Plant model that matches this specification:

Column Name Data Type
name string
image string
price decimal

If you use a Rails generator, don't forget to pass the --no-test-framework argument!

After creating the Plant model, you can run rails db:migrate db:seed to run your migration and add some sample data to your database.

Check your progress by running rails c and verifying that the plants were created successfully!

Routes

Your API should have the following routes as well as the associated controller actions that return the appropriate JSON data:

Index Route

GET /plants


Response Body
-------
[
  {
    "id": 1,
    "name": "Aloe",
    "image": "./images/aloe.jpg",
    "price": 11.50
  },
  {
    "id": 2,
    "name": "ZZ Plant",
    "image": "./images/zz-plant.jpg",
    "price": 25.98
  }
]

Show Route

GET /plants/:id


Response Body
------
{
  "id": 1,
  "name": "Aloe",
  "image": "./images/aloe.jpg",
  "price": 11.50
}

Create Route

In your controller's create action, use strong params when creating the new Plant object.

POST /plants


Headers
-------
Content-Type: application/json


Request Body
------
{
  "name": "Aloe",
  "image": "./images/aloe.jpg",
  "price": 11.50
}


Response Body
-------
{
  "id": 1,
  "name": "Aloe",
  "image": "./images/aloe.jpg",
  "price": 11.50
}

Once all the tests are passing, start up the React app (using rails start) and explore the functionality to see how the routes you created are being used.

phase-4-rails-create-read-lab's People

Contributors

ihollander avatar lizbur10 avatar devfortrav 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.