Using Scaffold in Ruby on Rails
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:
rails g scaffold name_of_file attribute1 attribute2 attribute3
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.
rails g scaffold car make:string model:string year:string
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.