Day 9: A Review Day

Today was a hard day at work. I work with teen boys currently as a clinician at a group home. Teen boys will wear you out! In any case, a 10 hour day is rough. I got home late and could have said heck with it and went to bed after eating, but I am committed to doing this! Since I do have to sleep soon, I decided today would be a good review day.

I recently left a message over at about my journey and received this advice from Dain Miller, one of the hosts:

Writing the blog is genius, you learn faster when you teach. Make the focus of your blog teaching what you just learned (and not just ‘i did this today and then this and then this’) and you will retain it 2x better and 2x faster than your peers.

He has a good point. I have always done well with writing stuff I am learning, so I will be reviewing the code and techniques I am learning on here. It will be boring, but I feel blogging is as much personal as it is communicative to an audience. I will be reviewing Chapter One of Michael Hartl’s Ruby on Rails Tutorial.

First and foremost, I DID install Ruby on Rails environment on my Windows 8 PC. I decided, however, to try out the free IDE on Cloud9. So far, I love it. It is really nice so far. I still don’t know about paying for it this early in my journey, however.

My install was pretty seamless on Windows 8 using RailsInstaller. The walkthrough had me up and running in a few minutes. I will hopefully try out coding in it soon. For now, however, I will stick with Cloud9.

So, the first chapter of the Ruby on Rails Tutorial involves making a “Hello, World!” program. This is done by first creating the application using the command:

*** By the way, I will be including the $ symbol as where I am typing. It is the Unix-style command line prompt. ***

The first part of this indicates the version of Rails to use to create a skeleton app, this being version 4.2.0. Next, the “new” command tells the system to actually create the directories. Finally, the “hello_app” part names the directory.

Next, we modified the Gemfile. The tutorial has us modify the existing Gemfile that was installed above with a version that installs specific versions that Mr. Hartl wishes us to use in the tutorial. This was done by a simple copy and paste. Next, however, comes the command to install the gems. This is done first by ensuring in the command line that we are in the right folder using:

and then using the command:

This installs the gems specified in the gemfile. From what I understand, the command above to install the new application did this before, but after modifying the gemfile to specify the exact versions we wanted, we run it again to install these.

Next is starting the Rails server. This is done using the following command:

If you were on a system running on a local machine, “rails server” is all that is needed to start the server. The “-b $IP” and “-p $PORT” are there because Cloud9 requires these so it can dynamically assign an IP binding address and port number. At this point, Rails is running.

Next, the tutorial discusses the architectural model that Rails runs off of: model-view-controller. I am going to try to put the idea into my own words. A user is browsing the web and comes across a site running on Ruby on Rails. They make a request to the site. This request goes to the controller. The controller has to make a decision, depending on what the request is. If something simple is needed, a view is immediately sent back. However, if something dynamic is required, the controller interacts with the model, which communicates with a database to get the information needed. This is then passes on to the controller that renders it to a view and sends it on to the end-user. This may be off. I’m still learning!

Now we are going to make our first app. Basically, the app replaces the generic Rails page with one that says “hello, world!” This will be done by adding a controller action that renders the string “Hello, world!” on the page. This is done by first going to the application controller in the directory and adding the following code:

So, “def hello” defines the action we are about to ask for as “hello.” Then, “render text: “hello, world!” is given as the action to call when “hello” is asked for. Next, we have to go to routes.rb and add some code to tell the Rails router where to send the requests. This is doneby writing the following:

This tells Rails to send the root route to the hello action in the Application controller. We defined that action above. As expected, when open the page in a browser, “Hello, world!” is displayed.

Next, the tutorial goes into placing the application under version control. This allows us to track changes to the project’s code, to collaborate more easily with others, and roll back if mistakes are made. To do this, we use Git.

First, we need to set ourselves up with Git. This is done with the following:

Of course, I used my email. I get enough spam….

The first two lines are necessary to register yourself if publishing repositories publicly. The third line, “git config –global push.default matching” ensures forward compatibility with an upcoming Git release. The fourth line, “git config –global checkout” creates the ability to use “co” instead of the “checkout” command.

Now we are ready. First, we initialize the repository using the command:

Next, we add in all the project files using the command:

Next, we need to tell Git to keep the changes we have made. This is done by telling it:

The flag at the end, “-m” allows you to add a message for the commit.

After this, I went to Bitbucket and made an account, then created a private repository. I had to add in my ssh key from Cloud9 and then added it to Bitbucket. This made adding the repository I created so much easier. I used this command:

BitBucket actually tells you what commands to use to add BitBucket as the origin for the repository, and then to push everything up. They are essentially as follows:

Git also allows branching so that you can experiment without doing damage to the original app. This is done by using the following command to checkout the branch:

This tells git to checkout the branch, indicated by the “-b” flag and then names the branch “modify README.” Then we made some changes to the README file in the application to make it appear on BitBucket. First, we changed the name of the README file using the Unix command “mv.”

This command renamed the README.rdoc file to

Next, we edited the files using HTML markup. Then, we ran the command:

that showed we had made changes. Then we used this command:

to commit all of the changes made, indicated by the “-a” flag, and added a message using “-m” flag saying what changes were made.

Then we need to merge the branch into the master. This is done by first checkout the master with the:

command, and then merging them via the

This merges the branch titled “modify-README” with the master. Now that it is merged, we can delete the branch using the command:

where the “-d” flag stands for delete. Now, we can push the changes to BitBucket. This can be done this time with a simple:

Finally, we are ready to deploy the application to Heroku. After the setup of Heroku on the system it was very simple to deploy it. First, we create a space on Heroku for the app to live using the command:

and then we deploy using this command:

And that is it for Chapter One. This review took a long time, but I really think I understand better what I did the first time through.