RSS Log in
 

This is the final part of Installing Ruby on a Mac with a quick walkthrough on creating a simple Hello World Rails application.

 

Getting Started

Step 1 - Xcode, Homebrew and Git

Step 2 - Wget and OH MY ZSHELL

Step 3 - RVM

Step 4 - Ruby

Step 5 - Gems and Pow

Step 6 - Databases and Rails

_Hello World_

 

After going through all the steps above, we can now create our first RoR application to ensure everything is working correctly.

 

Create a new application

Open a new terminal session and navigate to a folder where you want your site to live (e.g. ~/Dev/Projects/).

Type:

$ rails new Hello

Output Summary:

create

run bundle install
Fetching source index for
http://rubygems.org/
Using rake (0.9.2.2)

Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

 

Side Note

A bundle is a set of gems that your Ruby application depends on and requires to run properly. The easiest way to manage such application dependencies is to use Bundler. When you create a new RoR app, Bundler runs automatically as part of the process: $ run bundle install. Any missing libraries are downloaded and installed in the default gems repository. One of the benefits of using Bundler is that you don’t have to download and install gems shared with other applications every time. Bundler can also help if an application has multiple dependency requirements for different deployment environment (e.g. Dev, Production, etc.).

 

If the script ran to completion successfully, you should see an empty Rails application in a directory called Hello.

Change to the Hello directory to continue working with your new RoR application and explore its content:

$ cd Hello
$ l

New Rails App

 

Create a new database

If you open the config/database.yml file in your text editor, you will see the database connection information.

development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000

As mentioned before Rails uses SQLite3 by default.

 

Let’s create a new empty database.

In the application working directory, use Rake (Ruby Make) - a task management utility - to create the database:

$ rake db:create

If you go over to the db folder, you’ll find that the development.sqlite3 and test.sqlite3 database files have been created automatically for you.

 

Start the server

Start up the Web Server by entering this command:

$ rails server

Output:

=> Booting WEBrick
=> Rails 3.1.1 application starting in development on
http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-12-08 23:08:45] INFO WEBrick 1.3.1
[2011-12-08 23:08:45] INFO ruby 1.9.3 (2011-10-30) [x86_64-darwin11.2.0]
[2011-12-08 23:08:45] INFO WEBrick::HTTPServer#start: pid=23720 port=3000

 

WEBrick is the default Web server used by Ruby on Rails to test applications in a development environment.

 

Run the application

Next open your Web browser and enter http://localhost:3000 in the address bar.

If everything is running correctly, you should see the Welcome aboard page:

Welcome aboard

 

Your Rails application is up and running!

 

To stop the server, enter CTRL + C in the terminal session running the server.

 

Create a simple page

We know the site is running but what about displaying some text?

 

Side Note

The Ruby on Rails framework follows the Model-View-Controller pattern or MVC for short. MVC is an architectural pattern that aims to isolate an application’s business logic from its user interface and provide a loosely coupled architecture to facilitate development, testing and maintenance. In MVC, Models represent the information of the application (data), the Views the UI for the application (HTML pages in Rails) and the Controllers coordinate the two (i.e. handle incoming Web requests, interact with the models for querying data, and pass data back on to the views for presentation).

 

Type in the command below in your terminal to create a Controller and a View.

$ rails generate controller home index

Output:

create app/controllers/home_controller.rb
route get "home/index"
invoke erb
create app/views/home
create app/views/home/index.html.erb
invoke test_unit
create test/functional/home_controller_test.rb
invoke helper
create app/helpers/home_helper.rb
invoke test_unit
create test/unit/helpers/home_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/home.js.coffee
invoke scss
create app/assets/stylesheets/home.css.scss

 

From the output above, we can see that one of the file Rails created is index.html.erb. Let’s edit this file:

$ xedit app/views/home/index.html.erb

Replace the existing HTML and put just one line of text to display:

<h1>Hello</h1>

To set this as the default home page, first remove the default page:

$ rm public/index.html

 

And then tell Rails to route incoming requests for the home page to our Hello page.

To do this, find the following line in the config/routes.rb file:

# root :to => 'welcome#index'

And add this line just below it:

root :to => 'home#index'

 

After saving the file, restart the server and point your browser to http://localhost:3000 as before.

You should now see the Hello message displayed:

Hello site

 

I would recommend you read the Getting Started with Rails guide to familiarize yourself with Rails’ other features.

 

RoR with Pow

You might be wondering whether we can run this site using Pow?

 

If you not already there, open a Terminal session and navigate to the application working directory (e.g. ~/Dev/Projects/Hello).

Stop WEBrick if it’s running (CTRL + C)

Restart Pow:

$ powder -r

Assuming you’ve already symlinked the directory (as shown in step 9.2), open a new Web browser window and go to http://hello.dev.

Chances is that you will see this error: LoadError: no such file to load -- bundler/setup

Pow Error

 

What the message above tells us is that Pow is trying to use the System Ruby (version 1.8 pre-installed on Lion)… and therefore it cannot find the required gems. But fear not, the solution can actually be found in the Pow User’s Manual. Assuming you’ve installed Ruby using RVM (as in step 5), Pow can run applications under different versions of Ruby thanks to an .rvmrc file than can be created with RVM. Creating .rvmrc files for individual projects lets you automatically load the correct Ruby development environment for each application.

 

First double-check where Bundler installed your application gems. If your terminal session is still in the application’s root directory, type:

$ bundle show rake

Output:

/Users/pascal/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2

 

As expected the application is using the 1.9.3-p0 gems:

Gems directory

 

So let’s create a project-specific .rvmrc file to instruct Pow to use that version.

$ rvm --rvmrc --create 1.9.3-p0

This is more or less equivalent to saying rvm --default use 1.9.3-p0 but specifically for that application.

 

Restart Pow either using Powder:

$ powder down
$ powder up

Or by creating a file named restart.txt in the tmp directory (cf. documentation):

$ touch tmp/restart.txt

 

And we’re…

Done

 

I hope this guide will be of use to some people and…

If you see any inaccuracies, things that should be added, removed or corrected, please either leave a comment or drop me an email. Thanks.


Comments are closed
© Copyright 2012 TheBooleanFrog Powered by: BlogEngine.NET|Credits|Subscribe via RSS

Follow

twitter linkedin linkedin rss

TheBooleanFrog

Programming sticky notes and other distractions...