What do Scaffolds Create in Rails?
Walk through all of the files generated by the Rails scaffold generator along with their purpose. This is also a good way to learn standard elements that should be included when building features manually.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now, let's go back and see what the scaffolds generator did to make all this possible.

Here is the screen again.

large

In the first line, the scaffolds generator invoked the function active_record and this function is used to handle connections to the database, run database queries and change the structure of the database. In this case, this function created the migration file, called it projects and gave a path to it. If you go to your db directory, you can see this file under the new migrate folder.

large

It also gave attributes to the table along with their respective data types. The timestamps attribute gives us the created and updated date and time for each record in the database. It will be present automatically in every scaffold-generated database, and you don't have to do anything specific to create them.
After creating the database file, it also created a models file for us, and you can see it in app/models path and the file is called project.rb For now, this file will be empty.

In the next step, the scaffold invoked the test_unit function. Unless you skip the unit test function, it will create automated test functions in the test folder.
Next, it invoked the function called resource_route to add a route to our files. If you go to the route file, you will see that it automatically placed resources:projects at the top. This lets the application know that all the RESTful interfaces and routes for this module.

large

If this route folder did not know about projects, it wouldn't have been possible for us to go to the browser and do all the things we did.
In the next step, scaffolds created a controller for us, and put in a majority of the code that we need. This is what projects_controller.rb looks like:

large

If you're familiar with object-oriented programming, the above code is self-explanatory. If you don't know, what this means is that your application has all the public methods available through the ApplicationController class. For example, before_action is a method that is available to us because it is inheriting from ApplicationController.
In this file, before_action calls the method set_project, and if you scroll further down, you can see this method.

large

If you notice, this method is private, so it can be called only within this file for the most part. Though there are other ways to go around this restriction, it is considered bad practice and I wouldn't recommend it.

Going back to the terminal, you can see that in the next step, scaffolds created all the views for us. You can see these files too, when you go to the views folder located inside the app directory. When you click on projects sub-folder inside the views folder, you can see the following files:

small

Out of this list, the _form.html.erb looks different because it is a partial file. In Ruby, when you create a partial file, it means other files can call this one when needed. For example, when you go edit.html.erb or new.html.erb you can see only the following code:

large

These files don't actually have any form, rather they are simply calling the _form.html.erb file. The code: <%= render 'form' %> does this calling for you. When you open this form file, this is where you can see all the code.

large

The idea behind this practice is to minimize code duplication. It follows the established programming principle called DRY, where D stands for "Do Not," R stands for "Repeat" and Y stands for "Yourself." In the above example, both the edit and new files use the same form, so there is no need to create each one separately. Instead, you can create a single file and call it as many times as you want.

Also, when you have all the code in a single file, it becomes easier to manage. Say, in the future, you add five more parameters to your database. When you have separate form code in each file, you have to go to each file to add these parameters. If you miss out on any file or have a typo, your application will crash. On the other hand, if you have just a single file, you can make whatever changes you want to this file, and it will automatically get reflected in others.

Going back to the terminal now, the next item that was invoked by scaffolds is unit_test. If you're wondering why scaffolds invoked this module again, it's because the earlier one was for models and this one is for controllers.

The next one is helper, and it created a file called projects_helper.rb. These are view helpers that allow you to add any type of custom methods. I use these files a lot when I want to add different types of enumerators. Let's say, I have a list of states or zip codes, I don't want to create a database table because I don't want to query it every single time I need this data. Instead, I can put this list in a helper file and call it whenever I want. The obvious advantage with using helper files is that it improves the speed of the application as database queries tend to take a longer time to process.

In the next line, scaffolds called the unit_test again and then, invoked jbuilder. These builders help to create APIs. In fact, one of the incredible aspects of Rails framework is that it allows you to create any API quickly and easily. In this case, this function will change our project into the json format, so you can even use this application as an API when needed.

The next line invoked assets, and this function creates any custom JavaScript files that we would need for the json APIs.

Next, scaffold invoked the coffee script. Essentially, coffee is a written form of JavaScript that makes it easier to read, and it also gives you a choice to use JavaScript as an object oriented language.

Next, scaffold created the scss file, and this is where you can put your styles.

So, that is everything that scaffolds generated for us. For some reason, if your application did not run the way I showed you, then make sure to update your schema file by running rake db:migrate because scaffold does not update your database. You have to do it separately.

You can also go to your schema file to see what it looks like. It should be updated like this:

large

I hope you now have a good idea of what a scaffold is and what it can do for you.