Using Scaffold in Ruby on Rails

Photo by Tianyi Ma on Unsplash

Introduction

We’ve all been there. Wasting copious amounts of time setting up the seemingly endless rails files just to find out something is terribly wrong. Now introducing the scaffold command. Once you’re in the correct directory you can slap this command into your terminal:

This command will generate:

  • Controllers
  • Models
  • Routes
  • Test-related files
  • Filled out views(including forms)

Keep in mind that if you make a mistake you can replace the “g” that stands for “generate” for a d for destroy. This will destroy all the files created by the original generate command. In this case i’ll be using the following command.

Model

Tables

Controller

Views and Forms

Checking Routes

As you can see scaffold has created so many of the things that took us quite a while one by one. If we run rails routes in the terminal we can see the routes created for us as well.

Migrating and Seeding

Before running “rails s” to check out our local host we must first migrate our new created tables. I’ve seeded the database with some dummy data for testing.

Webpages

As we can see, we’ve been given not only simple webpages with working show, edit, destroy links but also forms pages:

Conclusion

Whether you’re looking to make a full website or need to make a quick draft for idea, rails scaffold will surely make your life a bit easier.

Coding Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store