Rails Scaffold Basics
By: John Ex
Tags: Ruby, Ruby On Rails Generators, Ruby on Rails, Scaffold, Rails

Rails is powerful and with the right amount of studying and practice, you can make some really great websites. As a beginner it can be extremely confusing to understand what exactly you’re doing and what the results will be. This is where scaffold, in my opinion, serves as a fun little tool to introduce the basic CRUD functions. In this blog I will also introduce how to set up a basic CRUD site that anyone can follow along to. Rails uses a mixture of many languages but today we will be talking about the concepts and basic rails commands.

CRUD

Before we start, what is CRUD?

C - Create

R - Read

U - Update

D - Destroy

So these are the basic functions of an app, especially when dealing with databases. You’re not expected to understand this now but you will get it by the end.

Creating a rails folder

To make your first rails application, you need to make a folder with the name you want. to do this open your terminal and cd into the desired location.(!Keep in mind this tutorial is for MAC and ruby + rails is installed!). Now type:

rails new name

with ‘name’ replaced with the folder name you want, for example:

rails new army

and when you hit enter you will see A LOT of things being created. Don’t be afraid, we aren’t even going to open that yet. You can type in

ls

or

open .

to make sure your folder was created with the correct name. If not delete whatever it did make and try again. lastly cd into it like this:

cd army

Now if you type ls, you’ll see all those files. Just to make sure all the gems are properly installed (if you don’t know what that means don’t worry), run:

bundle install

and once that is complete we are ready to actually make the website!

Generate Scaffold

Now is when you make decisions! The basic scaffold generator looks like this

rails generate scaffold Name attribute:type

If we break this down piece by piece it suddenly becomes very easy to make things.

|rails generate| just means use the rails command generate. can also be written |rails g|

|scaffold| of course is the type of generator which is going to make the basic CRUD stuff.

|Name| is going to be whatever you want to name the object you’re trying to make, in our case it will be soldiers.

|attribute:type| means we are adding an attribute and defining it’s type. For those who know, this creates a database migration which you can freely edit before migrating.

An example of this would be:

rails generate scaffold Soldier name:string age:integer

Keep in mind you can have as many attribute as you want and the attribute name can be named whatever you want. The types are predetermined however and if left blank will default to string. Once you are satisfied with the tables and names, its time to migrate. Run:

rails db:migrate

and

rails db:migrate:status

to make sure everything is working, signified by your tables being there and the Status being up.

Starting the Site

Finally, we can see what we have made! simply type in:

rails s

to start your server, then go to your browser and type in the URL bar:

on which you will see something like this:

This means you successfully made and ran your server. To see what you’ve made and use it add

/soldiers

or whatever name you made (make sure its plural in the link), to the end and hit enter. This should look like this

And that’s it! You can create new soldiers, edit them, delete them and see all their names and ages by navigating this new website you made. If you remember CRUD, this is how that concept applies to our test application:

C - Create - Create/Enter a new soldier with a name and age

R - Read - Readout/Show a soldier and his info and list all soldiers

U - Update - Update/Edit a soldier’s information like name and age

D - Destroy - Destroy/Delete/Remove a soldier from the website/database