Other Pages

CRUD With Scaffolding

Goals

    At the core, most database driven web sites are the same. They need to store records and provide a way to do the following:

    • Create new records in the database
    • Read or show the records in the database
    • Update existing records
    • Destroy or delete records

    Because these 4 actions (CRUD) are so common, Rails includes the scaffold command to make creating them easier.

Steps

Step 1

Without Docker
Type this in the terminal:
rails server
With Docker
Type this in the terminal:
docker-compose up

Now is a good time to figure out how to have multiple tabs or windows of your terminal or command prompt. Starting and stopping the Rails server all day is tedious, so it's good to have one terminal tab or window for running commands, and a separate one for the server.

Docker users should have a tab for each of the following:

  • docker-compose up for running the Rails server
  • docker-compose run web bash for the docker web service container, where you can run irb and all your Rails related commands
  • docker-compose down for shutting down the server or to interact with Git or Heroku

Step 2

Point your browser to http://localhost:3000/topics

You should see the "Listing Topics" page with headers for title and description, and a link to add a new topic:

Screenshot of topic list page

Step 3

  • Click "New Topic"
  • Fill in the form and click "Create Topic"
  • You should see a page showing your new topic with a message that your topic was successfully created:
Screenshot of topic detail page with confirmation message

Step 4

  • Click on "Back"
  • You should see the topic list again, this time with your new topic listed:

Screenshot of topic list with new topic

  • Try the "show", "edit", and "destroy" links to see what they do
  • You've created a basic database-driven web site. Congratulations!

Explanation

How did all those pages get created and hooked together? rails scaffold did it for you.