Rails! Here I come. (Day #2)
Tags: beginners, rails, writing
Published: 2019-01-29 15:34:03

---
title: Rails! Here I come. (Day #2)
published: true
description: On day two of my encounter with Rails, I try and get a better understanding of routes and controller.
tags: beginner, rails, writing
---

On the first day, I encountered a statement in the rails documentation that dealt with controller and routing. It read " A controller's purpose is to receive specific requests for the application. Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions. Each action's purpose is to collect information to provide it to a view." Now whilst, I did understand the first part of it, the latter part which dealt with routing made me go - "I have no idea what you are trying to communicate."  Therefore, I spent the better of today in trying to really internalize the concept of routing and controller. 

![MVC structure](https://www.bogotobogo.com/RubyOnRails/images/MVC/MVC_Rails.png)

An image that looked something like the one above gave me a pictorial insight on what was happening. I focused on the routing and controller part today and only slightly ventured in the model area today. 

How I went about understanding it? 

I tried creating a simple static page first that displayed static data. 

First, I set up the new rails application using `$ rails new blog `. This generated for me all the required files. 

Next up, I set up a controller and a view for the homepage by `$ rails g controller home index`. This command in the terminal set up a home controller for me. It also generated a home.scss of the stylesheet folder, and generated a home folder on views. Inside the home folder, it also generated an index.html.erb folder. Much of this was what I did yesterday.

I then went about configuring the routes to `get` index as the homepage. For this, I made changes to the routes.rb file.

```
Rails.application.routes.draw do
   get 'home/index'
   root 'home#index'
end
```
What is essentially going on here is rails via get method is getting `home/index` view and with root is setting the page as the homepage. Therefore, upon going to the localhost:3000 url, the contents of index.html will be display. However, before that can happen, I would still need to define  `/index` in the home controller. So I make changes to the `homecontroller.rb.`

```
Class HomeController < ApplicationController
  def index
   
  end
end
```
What I think is going on here is that the index is being defined, and although not mentioned the function is rendering the template `home/index` from the views folder. Therefore, we can see how the  `home/index` is routed to the controller. 

Upon firing up  `localhost:3000` after I have run `rails s` in the terminal, I see the contents of the index.html. 



![First page](https://i.postimg.cc/d0Jt74sK/rails-1-at-8-20-33-PM.png)

However, the data here was static, and this was also something I had done yesterday, albeit I had a better understanding now than I did yesterday. 

The next step was to try and get dynamic data. For this, I needed to set up a database. This was going to be my first rails database. I ran `rails generate model Post title:string author:string blob:text`. This set up a database for me with two table headings, title and body. The title and the body both being of text type. One final thing to do before the database was ready was to migrate it( I still need to figure out why and how this is important). For now, I was able to get the database set up with`rails db:migrate`. One important thing to note here is, the convention of naming a database in rails in the singular. Therefore, the database is named post, or user, etc and naming a controller is done in the plural, hence will be named posts or users.

 Next, I logged on to rails console so that I could manually insert data into the database. I will delve into getting data from forms tomorrow, for now, I wanted to manually insert the data and display them. `rails c` took me to the rails console. Although, I have to say getting to the console consumed a fair amount of time. I kept on getting various error messages, and I kept on troubleshooting, but once I had finally fixed it, I got the console. Now that I think of it, I think I should have documented the error messages I kept of getting. In the end, what I think worked was removing and reinstalling ruby. 

Back in the console, I ran `Post.all` command. This allowed me to view the contents in the database which right now had nothing in it. Therefore, I manually added contents to the database via `Post.create(title: 'Hello World', author: 'saral' , blob: 'This is the post')`and Post.create(title: 'Hello', author: 'Dikshant' , blob: 'Write this to the blog'). I had the database populated now. All I needed was to display them in the post view.

Now, I had to get the data from the database and display them on the homepage. For this, I could use the `Post.all` I previously used to view contents of the database from the console. On the Home controller is defined @posts. 

```
class HomeController < ApplicationController
    def index 
        @posts = Post.all
    end
end

```
Here, I have declared a global(instance) variable post and stored the data I get from Post.all in it. The @ denotes the posts is now a global variable. I could have declared a variable without @ but then I would not have had global access to them. 

Now, all that was left to do was make changes to the view to get the variables from the controller. 

```
<div class="container">
    <h1> Hello! World </h1>
    <table class="table">
        <thead class="thead-dark">
        <tr>
            <th scope="col"> Title </th>
            <th scope="col"> Author </th>
            <th scope="col"> Content </th>
        </tr>
        </thead>
        <% @posts.each do |post| %>
        <tr>
            <td> <%= post.title %> </td>
            <td> <%= post.author %> </td>
            <td> <%= post.blob %> </td>
        </tr>
        <% end %>  
    </table>
</div>
```

Now, I ran a loop inside the index.html.erb file using embedded ruby. I guess that is why the file extension for the html file is an erb, so that you can embed and run ruby commands. The loop `<% @posts.each do |post| %> ` ran a loop on the `@posts` variable and for each `post` printed out the data in a table using ` <%= post.title %> `. 

One key difference between the two embedded ruby codes is the use of `=` after `%`. The usage of `=` renders to display the contents. 

Finally, I generated the data on the homepage. 
![Displayed data from model](https://i.postimg.cc/MGPGBV33/display-at-9-15-21-PM.png).

I still want to have a better understanding of the loop in ruby. Although I feel I have an understanding of loops, the loops in ruby look a bit cryptic to me. So, I will look into that a bit more. Furthermore, I will explore and get data from forms and display them. That is for tomorrow.