RailsN00b: Journal of an Intern Rails Developer
Entry 1: The Obligatory "Hello, World!"
Tutorial Being Followed: "Instant Gratification" Ch. 2 of Agile Web Development With Rails 4. By: Ruby, Thomas, and Hansson.
Now that we have all the housekeeping out of the way, it's time to dive right in and make sure rails is working with the traditional "Hello, World!" test. If you rolled your eyes when you read that, you aren't alone; I also get tired of every single programming text starting with something so vanilla. So, let's spice it up a bit with a look at how Rails handles dynamic and a look at the Model-View-Controller structure of the Rails framework. Lets get started!
First we need to open up our terminal application and create a new Ruby on Rails application:
Move to whichever directory you want to create the application in. I will be creating a new director ( BlogProject ) as a subdirectory of my Work directory:
$ cd Work
$ mkdir BlogProject
$ cd BlogProject
Now we need to create a new Rails Application. I will be calling this application "demo" and use the Rake tool to examine it:
$ rails new demo
$ cd demo
$ rake about
Now we want to create (or, in Ruby on Rails, "generate") a controller called "Say" with two methods "hello" and "goodbye":
$ rails g controller Say hello goodbye
Now take a look at the new directory in your text editor of choice. Open up the file say_controller.rb found at /demo/app/controllers/say_controller.rb and add an instance variable for time in the "hello" method and user_name in the "goodbye" method. Assign time to the current time using Time.now and user_name to a string containing your name.
The resulting code should look something like this:
class SayController < ApplicationController
# Hello method
@time = Time.now
# Goodbye method
@user_name = "Carl"
This is a controller. If you are new to the MVC pattern, I suggest reading the wiki page: here.
When you created the controller and it's methods via the "$ rails g" command, you also created two "views" for each method. These views can be found in demo/app/views/say/.. with the extension ".html.erb"-- meaning HTML Embedded Ruby.
One of the cooler aspects of Rails framework is that you can inject ruby code into text the HTML by framing it with " <%= %>". You can also link pages to one another by utilizing the link_to() method which takes two parameters: a text to be displayed and a special variable in the format of class_method_path. Go ahead and add in a header and some text to these files, calling the appropriate instance variables inside "<%= ... %>". Also use link_to() to allow the user to move back and forth between the two pages.
The code should end up looking something like this:
<h1> Demo Program 1: </h1>
<p> Hello there, the time is now <%= @time %> </p>
<p> Find me in app/views/say/hello.html.erb </p>
<p> Say <%= link_to "Goodbye", say_goodbye_path %> </p>
Now it's time to launch the rails server and take a look at your code. To do so, simply open your terminal (make sure you are still in the demo directory). Launch the server with the following command:
$ rails s
Now open up your browser and go to http://localhost:3000/ and you will see the Rails "Welcome aboard" message. Now go to http://localhost:3000/say/hello and you will (hopefully) see something like this:
Congratulations! You have just created a very simple Rails applications. In our next lesson we will talk a little more about the third element of the Model-View-Controller structure-- the Model, and dive a little deeper into page flow in a Rails application.
By: Carl Bales, Twin Engine Labs
Date: 4 April 2014
ABOUT THE AUTHOR:
Carl Bales is an Intern at Twin Engines Lab in Shreveport, LA and a Cyber Engineering student at Louisiana Tech University in Ruston, LA. He has recently made the switch to Ruby on Rails after a few years of Python, Java, C/C++, and Perl programming. He writes these posts as a way of chronicling his descent into the Ruby on Rails programming framework and help others make an easy transition into this web-app development with Rails! He tweets at @RailsNoob and can be contacted via email at email@example.com .