Scaffolding: Make a Ruby on Rails App in just 5 Minutes

programming script


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:

`rails new bookstore` text

You have now created an app with the name bookstore. Next, change the directory to our bookstore.

cd bookstore

There are many models we should create for a new app when first starting to write the code like rails generate (or rails g).

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 title and author).

programming script

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:

programming script

Check out the output you will receive from that one line of code:

programming script

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 's "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:

ruby on rails string

Now we can take a look at the views and routes that were created. The bookstore should appear as:

local host books

There aren't any books yet, so we should create a book:

book title query

Click Create Book to create a book:

book title

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):

book title

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.

Are you a developer interested in growing your software engineering career? Apply to join the Andela Talent Network today.

Related posts

The latest articles from Andela.

Visit our blog

Customer-obsessed? 4 Steps to improve your culture

If you get your team's culture right, you can create processes that will allow you to operationalize useful new technologies. Check out our 4 steps to transform your company culture.

How to Build a RAG-Powered LLM Chat App with ChromaDB and Python

Harness the power of retrieval augmented generation (RAG) and large language models (LLMs) to create a generative AI app. Andela community member Oladimeji Sowole explains how.

Navigating the future of work with generative AI and stellar UX design

In this Writer's Room blog, Carlos Tay discusses why ethical AI and user-centric design are essential in shaping a future where technology amplifies human potential.

We have a 96%+
talent match success rate.

The Andela Talent Operating Platform provides transparency to talent profiles and assessment before hiring. AI-driven algorithms match the right talent for the job.