Generating a Blog for the Portfolio Application Using Rails Scaffolds
In this guide we walk through how to utilize the Rails scaffold system in order to create the blog for our portfolio application, including how to update the Postgres database.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now that we have the application created we can update our project management system. Open the item on PivotalTracker and click the first task to mark it as completed. Now we will move on to our next task.

In this guide we are going to walk through the details of an amazing rails tool called a scaffold. In rails, a scaffold is a process that allows you to generate multiple items at all the same time. This procedure shows the power of Rails and how quickly you can get from an idea to actually building a feature in your application.

The command for our feature is: rails g scaffold Blog title:string body:text

Here, we are creating a scaffold called Blog. It takes two attributes, a title of data type string and a body of data type text. If you are new to rails this syntax might seem a bit foreign.

How the Scaffold Generator Works

You start with the word rails. The "g" stands for generator. “scaffold” tells rails what type of generator you're using. “Blog” is the feature name we want. Our first attribute is title and it has a datatype of ‘string’ (which has limit of about 150 characters). Our second attribute is the body with a data type of ‘text’ (the text datatype means we won’t have to worry about the length of our blog content).

The syntax has to be exact. If you swap words like ‘rails scaffold g’, the generator will not work.
(Developer’s note: the Syntax is rails g scaffold Featurename attribute:datatype . You can add as many attributes and accompanying datatypes as your feature needs).

If you run our command, this should be your output.

large

What the Scaffold does for us

The scaffold system creates a lot of code. At a high level we can see everything the scaffold created:
- a database migration file (updates the database)
- a model file (a connection between the database and the code)
- routes
- view files
- javascript files
- stylesheets.

The Migration File

Let's start by looking at our migration file.

Open Sublime text choose File < Open and navigate to your Portfolio and click the open button. You should see the file tree on the left hand side. Click on the dbdirectory and then the migrate subdirectory. You will see there is a single migration file there. Simply click on the file to open it in the text editor

The migration file should look like this:

large

The generator took our command and created code for us. Here the scaffold created a blogs database table. You can see the attributes we asked it to create of title and body. They are of the same data type that we specified at the time of creation. By default, Rails has also created another attribute called timestamps. The timestamp is a handy built in way to keep track of the date and time when a blog was created. It will also create an updated timestamp if you edit your blog. This is very useful for the management of your items.

Running the Migration

To get this migration to work, we need to go back into the terminal and type the command
rails db:migrate
When we hit return this will run the migration and update the database, ie: it will create the table in the postres database for us.

The Schema File

To check if it worked, go to your db folder and open schema.rb. It should have your title, body, and the created and modified timestamps.
As a rule, you should never change this file. Just use this file as a point of reference. You will check the schema file often to ensure your database tables and fields look the way you want.

Your Routes File

Remember you can use the fuzzy search feature in sublime to find the file you want to open. Hit command + ’t’ and start typing in ‘routes’ and you will be able to quickly access your ‘routes.rb’ file.

This file gives us access to resources :blogs. We'll go through what this means in detail in our routes guide, but be aware this line of code gives us all kinds of routes for our blog content. It will allow us to view our blog pages in our application.

Visit Your Very Own Blog Page!

Go back to the terminal and start your rails server: ‘rails s’

Open your browser. Go to the URL "localhost:3000/blogs", and you should see your Blogs page. You can see that the blogs feature has been created for us.

Once you enter your title and content and click the "Create Blog" button, you can see that the system actually saved the blog for you in the database. This is fantastic functionality. There is even a built in notification!

Click the back button and you can go to your blogs directory page, and see the record you created!

The show button will take you to a page with that specific blog. You can even edit your blog.

The Speed of the Scaffold

If you’ve come from another programming language, you can see that scaffolding is astronomically faster than writing the code from scratch. It could have taken days or weeks to build this CRUD functionality in another language. CRUD gives you the ability to create, read (see), update (edit) and destroy (delete).

This amazing aspect of Rails is why it is so popular with developers. It allows you quickly prototype applications and build out functionality. In this guide you have been able to see the far reaching affect the scaffold generator has on your application. In the next guide we will analyze everything that has been created so you have a better understanding of the process.