One of the things that makes Rails a great framework is the ability to connect to a “built-in” database.
A database is a place where you can store information. This can be user information, or anything else you would like to build a dynamic website.
Remember: what makes Rails different, is the ability it gives you to dynamically show data on your website (kind of like facebook. The data on the page changes bases on the user that is logged in).
This is possible because of Rails architecture, which follows the MVC model.
A quick recap
MVC stands for Model, View, & Controller. The model represents the data. The view is what users get to see (html, css, javascript), and the controller is the backend that controls how the model is viewed.
They all depend on each other in order to create a dynamic Rails website.
Below is an illustration of how MVC is used in Rails:
Hopefully that starts to clarify how Rails works.
Rails Database
Something we haven’t really talked about is Gems.
You might ask, what are gems?
Gems are packages that allow you to do different things.
Some allow you to create forms where users can input their email and password. Others allow you to store users information on a database.
They all come in different sizes and colors. You can think of them as building blocks of a Rails website.
For example, when you are building a house, you use different tools.
For your bedroom, you need glass for the windows. You also need blinders for the windows.
For the kitchen, you need a stove, and a fridge.
All of these items help you build a bedroom and kitchen.
Gems are kind of like those tools. They are different from each other but they make up a website as a whole.
Todays Gem — Sqlite3
Sqlite3 is a database gem that allows you to store information. We are going to be using it in order to create users for our rails app.
Step 1: make sure you have sqlite3 in your Gemfile (in your rails app, command + p and type Gemfile. Scroll down to the bottom and check that you sqlite3.)
Step 2: Create a new user
$ rails g model User email:string password:string
The above command will allow us to create User model that will have two columns: email, and password
$ rails c
This above command will allow you to get into the console so you can create dummy users on the fly.
$ > user = User.new(email: "abe@hello.com", password: "12345678")
After the above command, you will see a new user with the email and password. For now, the password will not be encrypted, so you will see what the password is. In most situations, you want to hide the password.
Change User Information
$ > user.email 'abe@hello.com'
The above command lets you know that have access to the user’s email if you user.email (same thing for the password).
$ > user.email = "zebo@hello.com"
check if the data changed:
$ > user.email'zebo@hello.com'
Step 3: show the user information on the view
In one of your controllers, find the index function. Inside the index function, enter:
def index
@user = User.last
end
In your, index.html.erb
<p> <%= @user.email %> </p>
<p> <%= @user.password %> </p>
You will able to see that you database is now connected to the view via the controller.
This completes the MVC cycle in a Rails application. Go back and review this thread and see how each file (controller, model, & view) is used to create a dynamic website.