Learning Rails: Application Generation

Table of Contents

In the last two posts, I installed Rails on both the Debian and Windows boxes.  If you want to read about how I did it and the problems I had to solve then check those posts out.  Now that Rails is finally installed I am ready to start writing my URL shortener.

Rails is a Model, View, Controller, or MVC, architecture. The models represent the data, and are commonly tied to the database objects. Views define the user interface of the application. Controllers provide the business logic by processing the requests, interacting with the models and then passing the data to the views for rendering. The MVC architecture isolates the user interface from the business logic, helping to create more maintainable code.

Generate the Application

The rails command is the heart of the Rails environment. It is used to create new projects, generate code, and run and debug the application. To start a new Rails application I’ll use the rails new APPNAME command, which generates the skeleton of a new application in the APPNAME directory.

$ rails new njeni.us
 create
 create README
 create Rakefile
 ...snip...
 Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
$ cd njeni.us

Read the rest of this entry »

Learning Rails: Installing Rails, Part 2 – Windows

Table of Contents

Last time, I installed Rails on the Debian box and had some trouble, but I got through it. This time, I’m going to install Rails on the Windows box.  This is my laptop that I have with me most often, so I need to develop from it, too.

Installing Ruby and RubyGems

To begin, I need Ruby on the machine, and luckily there is a Windows installer available. The RubyInstaller project is:

…a self-contained Windows-based installer that includes the Ruby language, an execution environment, important documentation, and more.

That’s handy. I download the latest RubyInstaller from here and run it.  I make sure to check Add Ruby executables to your PATH so I can use ruby and gem from the command line.

When it finishes, I open a command prompt to make sure it installed correctly.

C:\Users\Miller>ruby --version
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]

RubyInstaller also installs RubyGems automatically, but it’s an older version.

C:\Users\Miller>gem --version
1.7.2

I update to the latest version using the gem update --system command.

C:\Users\Miller>gem update --system
Updating rubygems-update
...snip...

C:\Users\Miller>gem --version
1.8.10

Installing Rails

The easiest way to install Rails is with RubyGems.

Read the rest of this entry »

Learning Rails: Installing Rails, Part 1 – Debian

Table of Contents

Before I can write code, I need to install Rails.  The best place to get started is the Ruby on Rails Download page. I work on both a Windows box and a Debian box, so I’ll need to install Rails on both machines. This time, I’m going to focus on installing Rails on the Debian box, which is actually the server where this project will eventually run. When I’m doing a lot of command line work, I prefer to work on the Debian box.

Installing Ruby and RubyGems

Note: I’m running a clean install of Debian Squeeze for this project. If you aren’t, your steps might differ. Follow the instructions at the Rails download page.

First, I install ruby.

$ sudo apt-get install ruby
Reading package lists... Done
...snip...
Setting up ruby1.8 (1.8.7.302-2squeeze1) ...
Setting up ruby (4.5) ...

$ ruby --version
ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]

That looks good. Now I need RubyGems.

RubyGems is the standard package manager for ruby.  If you need a Ruby library, like Rails, there’s a good chance it’s available as a gem. So, I download and install the latest version.

$ wget http://rubyforge.org/frs/download.php/75309/rubygems-1.8.10.tgz
...snip...
2011-10-19 21:49:25 (351 KB/s) - `rubygems-1.8.10.tgz' saved [249683/249683]

$ tar -xzvf rubygems-1.8.10.tgz
...snip...

$ cd rubygems-1.8.10

$ sudo ruby setup.rb
RubyGems 1.8.10 installed
...snip...
RubyGems installed the following executables:
        /usr/bin/gem1.8

That was pretty easy.

Read the rest of this entry »

Learning Rails: Introduction

Table of Contents

I’ve wanted to learn Ruby on Rails for a few years.  I like the simplicity and elegance of the framework and the resulting projects.  I have started to learn it in the past, but I always end up quitting because I don’t have something specific to work on.  This time I am starting with a plan to build something useful, and post the progress for the world to see.

The Plan

I will create a new project from scratch using Ruby on Rails and document the journey here.  For me, there is no better way to learn about new technology than to create something useful and write about it.

Along the way, we will cover:

  • Creating a project with rails.
  • Implementing something useful.
  • Writing tests.
  • Source control.
  • Deploying the application.

I will cover each step in the process with an individual post and I will try to keep everything short and simple.  I will deploy early and often.  Each version will be deployed separately and linked from the relevant posts so you can see the progress as we go.

The Project

I will create a URL shortener, njeni.us.  Why a URL shortener?  It’s a relatively simple project that I think fits well into this plan. It’s something I will actually use when I post links on twitter. And finally, it’s just plain cool to have my very own vanity short URLs!

I’m sure the project will evolve as I go, but here are the features I would like to see in the project.

  • Create a short URL.
  • Redirect clicker to site.
  • AJAX
  • Implement an API.
  • Track and display statistics.

Follow Along

If this sounds interesting, follow along.  You can see all the posts in this series at the Table of Contents, or subscribe to the blog to get the latest posts as soon as they are written.

I’d love to hear your comments and suggestions, so please let me know what you think.