Learning Rails: Resource Generation

Table of Contents

(Coming Soon!) Validations and Testing »

Last time, I saw how easy it was to create a new application using the rails new command. I learned a bit about controllers, views and routes, including how to create my own. Today, I’m going to learn about resource generation and data models, and create a crude URL shortener.

Generating Resources

A Rails application is organized around resources. A resource is simply the object the user of the application will interact with. I can let Rails generate my resources for me using the rails generate scaffold command. Invoke the command by passing the name of the new resource and a list of the properties. In this case, I am creating the ShortUrl resource with a single string property named url.

$ rails generate scaffold ShortUrl url:string
      invoke  active_record
      create    db/migrate/20111029050110_create_short_urls.rb
      create    app/models/short_url.rb
      invoke    test_unit
      create      test/unit/short_url_test.rb
      create      test/fixtures/short_urls.yml
       route  resources :short_urls
      invoke  scaffold_controller
      create    app/controllers/short_urls_controller.rb
      invoke    erb
      create      app/views/short_urls
      create      app/views/short_urls/index.html.erb
      create      app/views/short_urls/edit.html.erb
      create      app/views/short_urls/show.html.erb
      create      app/views/short_urls/new.html.erb
      create      app/views/short_urls/_form.html.erb
      invoke    test_unit
      create      test/functional/short_urls_controller_test.rb
      invoke    helper
      create      app/helpers/short_urls_helper.rb
      invoke      test_unit
      create        test/unit/helpers/short_urls_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/short_urls.js.coffee
      invoke    scss
      create      app/assets/stylesheets/short_urls.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss

The scaffold generator created everything I needed to start working with my resource including the controller, routes, views, model and database migrations.

Read the rest of this entry »

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 README
 create Rakefile
 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 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
Setting up ruby1.8 ( ...
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
2011-10-19 21:49:25 (351 KB/s) - `rubygems-1.8.10.tgz' saved [249683/249683]

$ tar -xzvf rubygems-1.8.10.tgz

$ cd rubygems-1.8.10

$ sudo ruby setup.rb
RubyGems 1.8.10 installed
RubyGems installed the following executables:

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.