The Magic of Scaffolding in Rails

Grant Hesketh
2 min readMay 28, 2021

--

Rails is an awesome web framework that has all kinds of magic sprinkled into it. Built on Ruby, rails allows you to create RESTful API’s super fast and almost without thinking. The fastest way to do this is with a fun little trick called scaffolding.

Scaffolding is a macro built into rails that creates all of the base files you need to run an API on rails and even populates the routes and methods needed to access whatever data or models you want to create. So let’s do some scaffolding and see what it can do.

Start with a base rails project and then in your console enter the following command:

And thats it! You have just created an API. Let’s look a little deeper and see what happened. If you look in your directory of the project you will notice a few things:

  1. A controller for Movies (MoviesController)
  2. A model for Movie
  3. A route for all Movie resources in your routes.rb file
  4. Some testing-related files

Wow! That’s a lot of work you don’t have to do. Let’s take a quick look at the controller file and see the bonus lines of code that scaffolding gives us as well.

As you can see all the base methods for accessing the “movie” data via http requests is all there for you! The only thing to worry about is falling into the trap of complacency where boilerplate code may not suit your needs in which case the most prudent option is to just write it yourself. But at least scaffolding will get you 90% of the way there!

--

--