Ruby on Rails is a great resource for building an app. Whether you’re a first time user of Ruby and are working on getting a new app running, or you’re a veteran of the Ruby platform, quickly creating an app (in 5 minutes or less) can be done using scaffolding.
But what exactly is scaffolding?
In Ruby on Rails, the term scaffolding is an auto-generated simple set of models, views and controllers. They generally apply to one table.
Why you should be using scaffolds
- You can use scaffolds as a tutorial on how Rails works by looking at the existing code written in the scaffold.
- You can quickly and efficiently make an MVP or demo of a new app so that you can show potential users or investors to get their feedback.
- Sometimes it’s just nice to be able to create an app in 5 minutes.
That’s enough chit chat, let’s get to the coding!
Begin by making a Rails app. Let’s say, for this example, we make an online bookstore. Run the following command:
You have now created an app with the name bookstore. Next, change the directory to our bookstore.
There are many models we should create for a new app when first starting to write the code like
rails generate (or
If you consider that the most important resource for a bookstore app will certainly be books, we should concentrate on those first.
We should make a model and controller for books (the attributes we will be using are
For the next step, write the views in the app/views folder, followed by defining the routes in the config/routes.rb, etc.
By using scaffolding, all of these processes can be accomplished in one simple step.
Let’s check out scaffolds
We can automagically create all of the data we need from one easy command:
Check out the output you will receive from that one line of code:
Not only have the model, views and controller been created automatically, there have also been test files made for the models and views; the books each have helper files, a Coffeescript file has been created to content the front end logic, styling is contained within sass files, and there is even a ‘books’ resource available in the routes file to define all available routes for the books.
Don’t just take my word for it. Spend a few minutes looking over all of the files created. You may even want to investigate which code they contain so that you can get a better understanding of exactly what is happening in the background.
If you’d like, you can find out which routes have been created with the command resources by running a
rails routes command.
View the new app
The books resource has some basic views that were set up by the scaffold.
Since we created a new model, we need to run migrations and then start the Ruby on Rails server:
Now we can take a look at the views and routes that were created. The bookstore should appear as:
There aren’t any books yet, so we should create a book:
Click Create Book to create a book:
Below you can see the list of created books (I went ahead and added another book by Jane Austen, because in my opinion no bookstore is complete without one):
Looking at our current list of books, you will notice that we have routes for show, edit and destroy. These routes will allow you to view a book, edit the contents or delete the book.
That’s it! With just a few simple commands, we have created a basic bookstore app that is ready to use complete with a visible sample of the book resources all using the Ruby on Rails platform.
Feel free to modify anything we have worked on today, including models, views and controllers, to make your own unique bookstore. Or better yet, use what we have learned on any sample apps you are working on in the future.