Building a Ruby on Rails Application on Mac OS X – Part 1

 

Screen Shot 2015-05-24 at 1.30.00 PM
The application we are going to build is a blog. This article assumes that you already have the following installed:
  • Ruby language version no older than 1.9.3
  • The Ruby Gems packaging system. This is installed with the Ruby version 1.9, and any more recent version.
  • SQLite3 Database
  • Ruby on Rails development framework

 

Building a Functional Rails Application

Now we can start to build the application.

Open your terminal and make a directory for your project. Go to your new directory. Type in the following:

$ rails new blog

Press enter. A long list of files appears in the terminal.

Screen Shot 2015-05-24 at 1.26.57 PMThe rails application ‘blog’ has been created, and the gem dependencies have been installed. Now switch to the blog folder by typing the following:

$ cd blog

The rails application ‘blog’ has been created.

Starting up the Web Server

 

Now enter in terminal:

$ bin/rails server

Some text will appear on the screen indicating WEBrick has started. WEBrick is a web server distributed with Ruby.

Screen Shot 2015-05-24 at 1.26.25 PM

Open a tab on your web browser and enter:

http://localhost:3000

You should see the Rails default information page, which means everything has gone right so far.

 

Screen Shot 2015-05-24 at 1.30.00 PM

Making the Controller and the View

 

We want to change our Rails default information page to say “Hello Rails” instead. In order to do this we need to create two things:

  • a controller
  • a view

 

A  controller’s job is to receive the application’s requests. There is something called “routing” which makes sure that the right requests are sent to the controller they are intended for. There are often multiple routes to each controller. For each of these routes, there are actions that serve it.  The action‘s job is to collect the information from the controller to provide it in a view.

A view‘s job is to show the information from the action, in a format that humans can read. This information is collected in the controller though, not the view. The view just displays the information. The view templates are automatically written in eRuby. eRuby(Embedded Ruby) is processed in the Rails request cycle, then it is sent to the user.

But first we need to stop the web server before we can proceed any further. While you are in terminal, hit:

Ctrl + C

Now the server is stopped and you see the command prompt cursor again.

We are going to use the controller generator to create a controller. We will tell it we want a controller called “welcome” with an action called “index”.

$ bin/rails generate controller welcome index

Rails creates several files and a route.

Screen Shot 2015-05-24 at 2.54.25 PM

I am using Sublime Text as my text editor. I want to open my text editor to look at the directory files. One way to open it easily is by entering the following in the terminal:

$ subl .

Sublime Text opens automatically, right at the correct directory. Open the app/views/welcome/index.html.erb file in your text editor. You will see this. Screen Shot 2015-05-24 at 3.38.55 PMDelete all of this code. Write the following to replace it:

<h1>Hello, Rails!</h1>

Save it. Screen Shot 2015-05-24 at 3.48.30 PM

We now need to tell Rails where we want the “Hello, Rails!” to be seen.  We want it to show up when we go to http://localhost:3000. Right now it says “Welcome Aboard”. We want to tell Rails where the actual home page is located.

Open config/routes.rb in the editor.

Screen Shot 2015-05-24 at 4.57.00 PM

The routes.rb file is the application’s routing file. It holds entries in a special domain-specific language (DSL). The routing file tells Rails how to connect the incoming requests to controllers and actions. You see there are many sample routes that are commented out with the hashmark(#).

Find this line:

# root ‘welcome#index’

Remove the # from the beginning of the line to uncomment it. This will make it active. It should look like this now:

root ‘welcome#index’

The ‘root ‘welcome#index” commands Rails to map requests received at the root of the application to the welcome controller’s index action. The ‘get ‘welcome/index” tells rails to map requests received at http://localhost:3000/welcome/index to the welcome controller’s index action.

Launch the web server again with bin/rails server and open http://localhost:3000. You will now see “Hello, Rails!”. This means that the new route is going to WelcomeController’s index action and creating the view.

End of Part 1.

Leave a Reply