DEV Community

Saral Karki
Saral Karki

Posted on

Rails! Here I come. (Day #1)

Finally, after months of toiling, I am close to finishing my first ever MOOC - CS50's Introduction to Computer Science on edx. It has been a great learning experience, and I have seen my share of frustrations, and have had moments of sheer joy after finishing a certain task or problem set. At last, I am writing my final piece of code as a part of this course, and, honestly, I can't wait to get certified.

This brings me to Rails. I intend to write and deploy my app using rails. I have heard a lot about rails and how it boosts productivity, and the ease with which web apps can be deployed. Also reading this post by Ben helped me get a better insight on Rails. Whilst I am not imagining myself to start producing web apps after web apps right now, I do want to be able to do that sometime in the near future. Mind you, I also have no hands-on experience on ruby, but I am counting on my experience with using to C to facilitate my learning of ruby.

Without further ado let me get into what I got into doing today:

  1. Installation and setting up Rails

    First I set about setting up Rails on my system. For this, I followed the documentation available on the rails site, and soon I had rails on my system.

  2. Creating my first application

    Next, I went about trying to set up a static page. From my earlier projects, I had a basic working idea of the model, view, controller (MVC) approach, and it was reassuring to know that Rails had a similar approach to development.

    1. Create the first files:

      rails new blog was all I need to write in the terminal for Rails to quickly generate all the files that were required for me in a folder called blog. The filename for the new file could be anything as per the requirement, for me, in this case, it was a blog.

    2. Running the server:

      Finally, I ran the local server by rails server on port:3000 which is the default port for Rails. And as I browsed to the localhost:3000, voila - I had got the server up and running.

      Rails server running

  3. Setting up a Static page

    Next, to set up a static page, I at least needed a controller and a view. A controllers job here is to receive specific requests for an application. The purpose of a view to display information to the human. The controller is where the information is collected, and the view merely displays that information. Then there is also routing which determines which controller receives which request, and often there is more than one route to each controller. I am merely paraphrasing the documentation here(as for now), but because there are so many fundamentals to unpack, I want to have them written down just so I get a better understanding of them.

    To generate a controller and its associated view we can run `bin/rails generate controller home index.

    1. Html file

      In this case rails generates a controller home, and a home folder in the views folder. There's a lot of other files that the command generates, but for now we focus on these two. The html file generated is a bit different to your normal html file, meaning that the extension of the file contains erb. This means you will be able to embed ruby commands to your html files giving you more control.

      I can then make changes to my index.html.erb file which resides in my view>home folder .

    2. Route changes

      I then head to my route.rb on the config folder. Here, I change my code to get my home/index and set the root to home#index.

      Rails.application.routes.draw do
      
          get 'home/index'
      
          root 'home#index'
      
      end
      

Essentially what I am doing here is asking my route to get the index from home, and setting my root to the homepage, which in this case is index.html.erb.

Now, if I refresh my page , my homepage has changed to the my index page.

There is a lot for me to unpack from rails, but it has been a good day. I did have to troubleshoot my way at times due various errors, but most of the solution that I implemented was via a quick search, and many times I had no clue what I was doing. It was good to get rails set up and run my first static page.

A small victory!!!

Top comments (3)

Collapse
 
briankephart profile image
Brian Kephart

I also took CS50 before learning Rails. It’s been great for me, and I hope it’s the same for you!

Collapse
 
saral profile image
Saral Karki

Thanks. :)

Collapse
 
clara profile image
Clara

How is your Rails journey going? I did it the other way round, first learned Rails and now I am currently doing CS50. Halfway through so far, and man it can be frustrating