Ruby Tutorials Notes, Links and other Goodies

Ruby and Ruby on Rails tutorials and related goodies

Ruby Tutorials Notes, Links and other Goodies (tutorials.shtml) | Updated: 23-Sep-2008 - 14:54

Rails Minimum click to show or hide section

The minimum that is required to get a Ruby on Rails site running on your local computer.

Ruby, Rails, and Leopard click here to hide or show section

The following should already be installed is running OS X 10.5 Leopard:

  • Ruby
  • Ruby gems
  • Ruby on Rails
  • SQLite3

In Leopard and Leopard Server, Ruby and Rails are pre-installed along with a bounty of other useful RubyGems. This means we can hit the ground running when it comes to developing a Rails application and keep up the pace when deploying new releases of our application to Leopard Server.

Out of the box, you get Ruby version 1.8.6 and Rails version 1.2.6, the latest stable releases at the time Leopard shipped. Ruby releases are few and far between (it's still at 1.8.6), but Rails has frequent new releases. In fact, the application we'll build requires Rails 2.0.2. The good news is it's easy to upgrade Rails and RubyGems. Make sure your system is up to date now by running these commands:

  • $ sudo gem update --system
  • $ sudo gem install rails
  • $ sudo gem update rake
  • $ sudo gem update sqlite3-ruby

The first command updates the RubyGems system itself, which is required by the latest version of Rails. The second command updates Rails-specific gems and installs any new components (specifically Active Resource). The third command updates Rake, the make-like build tool used by Rails. Finally, the last command updates the Ruby bindings for the SQLite3 database. Running all these commands may take a while. When it's finished, you can list all the installed gems by typing

$ gem list

Creating a New Rails Application click here to hide or show section

$rails todo
$cd todo
$rake db:create:all
$script/generate scaffold Todo title:string body:text done:boolean due:datetime
$rake db:migrate

Then start the web server

$script/server

Open your browser to http://localhost:3000/todos

Rails 2.0 and Scaffolding Step by Step by Sean Lynch click to show or hide section

My travels in learning Ruby, Ruby on Rails on Mac OS X - Leopard 10.5

While working through a book on Ruby on Rails 1.2 you will have trouble getting something up and running as there have been changes in Rails 2.0. I did. So with the wonders of the web and fellow Ruby explorers that come before us one can track down what is need to get things going.

This is a multi-part tutorial covering enough to get a scaffolded Rails application up and running under Rails 2.0.

Based on Sean Lynch's tutorial at: fairleads: Rails 2.0 and Scaffolding Step by Step tutorial

Ruby, Rails, and Leopard click here to hide or show section

The following should already be installed is running OS X 10.5 Leopard:

  • Ruby
  • Ruby gems
  • Ruby on Rails
  • SQLite3

In Leopard and Leopard Server, Ruby and Rails are pre-installed along with a bounty of other useful RubyGems. This means we can hit the ground running when it comes to developing a Rails application and keep up the pace when deploying new releases of our application to Leopard Server.

Out of the box, you get Ruby version 1.8.6 and Rails version 1.2.6, the latest stable releases at the time Leopard shipped. Ruby releases are few and far between (it's still at 1.8.6), but Rails has frequent new releases. In fact, the application we'll build requires Rails 2.0.2. The good news is it's easy to upgrade Rails and RubyGems. Make sure your system is up to date now by running these commands:

  • $ sudo gem update --system
  • $ sudo gem install rails
  • $ sudo gem update rake
  • $ sudo gem update sqlite3-ruby

The first command updates the RubyGems system itself, which is required by the latest version of Rails. The second command updates Rails-specific gems and installs any new components (specifically Active Resource). The third command updates Rake, the make-like build tool used by Rails. Finally, the last command updates the Ruby bindings for the SQLite3 database. Running all these commands may take a while. When it's finished, you can list all the installed gems by typing

$ gem list

Creating a New Rails Application click here to hide or show section

$ rails exchange

Next, open the new project directory with TextMate by typing mate exchange or dragging the new project folder to the TextMate icon in the Finder or the Dock.

$ mate exchange
Highslide JS
TextMate project with controller application.rb in edit window
$ cd exchange
$ script/server exchange

Now point your web browser at http://localhost:3000

Highslide JS
Ruby on Rails - About your application’s environment

Creating a Database click here to hide or show section

The following database servers are currently supported by Rails: MySQL, PostgreSQL, SQLite (SQLite3 is the default), SQL Server, DB2, Firebird, and Oracle.

Highslide JS
TextMate project with db config/database.yml in edit window
$ rake db:create:all

db:create:all Create all the local databases defined in config/database.yml (above )

rake Command
  • db:charset Retrieves the charset for the current environment’s database
  • db:collation Retrieves the collation for the current environment’s database
  • db:create Create the database defined in config/database.yml for the current RAILS_ENV
  • db:create:all Create all the local databases defined in config/database.yml
  • db:drop Drops the database for the current RAILS_ENV
  • db:drop:all Drops all the local databases defined in config/database.yml
  • db:reset Drops and recreates the database from db/schema.rb for the current environment.
  • db:rollback Rolls the schema back to the previous version. Specify the number of steps with STEP=n
  • db:version Retrieves the current schema version number

script/generate scaffold post click here to hide or show section

$ script/generate scaffold Movie title:string description:text one_sheet_url:string

Rails 2.0 is REST (Representational State Transfer) by default. The usual suspects are created: Controller, Helper, Model, Migration, Unit Test, Functional Test

The scaffold command contains the database "Movie" and the columns that added. The columns can also be added by hand.

Highslide JS
TextMate project with db db/migrate/001_create_movies.rb in edit window

Database Migration click here to hide or show section

$ rake db:migrate
Curt:exchange curt$ rake db:migrate (in /Users/curt/dev/exchange) == 1 CreateMovies: migrating ================================================== -- create_table(:movies) -> 0.0021s == 1 CreateMovies: migrated (0.0022s) =========================================
<$ script/server exchange

Now point your web browser at http://localhost:3000/movies

Highslide JS
The results of: http://localhost:3000/movies in Safari
Highslide JS
Terminal window capture with successful web browser rendering

At this point you can follow part 1 of Sean Lynch's the tutorial at

Summary click here to hide or show section

  • $ rails exchange
  • $ rake db:create:all
  • $ ruby script/generate scaffold Movie title:string description:text one_sheet_url:string
  • $ rake db:migrate
  • $ ruby script/server
  • http://localhost:3000/movies

This was the first time I could get Rails 2.0 running on om my Mac. The only problem I had with the tutorial was in part 2 and the validates_format_of code. I was getting an error I can't figure out in the :with

Developing Rails Applications on Mac OS X Leopard click to show or hide section

developer.apple.com tutorial wuing XCode 3.0

Ruby, Rails, and Leopard click here to hide or show section

The following should already be installed is running OS X 10.5 Leopard:

  • Ruby
  • Ruby gems
  • Ruby on Rails
  • SQLite3

In Leopard and Leopard Server, Ruby and Rails are pre-installed along with a bounty of other useful RubyGems. This means we can hit the ground running when it comes to developing a Rails application and keep up the pace when deploying new releases of our application to Leopard Server.

Out of the box, you get Ruby version 1.8.6 and Rails version 1.2.6, the latest stable releases at the time Leopard shipped. Ruby releases are few and far between (it's still at 1.8.6), but Rails has frequent new releases. In fact, the application we'll build requires Rails 2.0.2. The good news is it's easy to upgrade Rails and RubyGems. Make sure your system is up to date now by running these commands:

  • $ sudo gem update --system
  • $ sudo gem install rails
  • $ sudo gem update rake
  • $ sudo gem update sqlite3-ruby

The first command updates the RubyGems system itself, which is required by the latest version of Rails. The second command updates Rails-specific gems and installs any new components (specifically Active Resource). The third command updates Rake, the make-like build tool used by Rails. Finally, the last command updates the Ruby bindings for the SQLite3 database. Running all these commands may take a while. When it's finished, you can list all the installed gems by typing

$ gem list

Creating a New Rails Application click here to hide or show section

Based on the Apple Developer's tutorial Developing Rails Applications on Mac OS X Leopard

$ rails expenses

Highslide JS
XCode Organizer with controller application.rb in edit window

Add a s shell script for script/server as a Top Level Organizer Item to the expenses folder. Or, using the Terminal application start the server $ script/server

Now point Safari at http://localhost:3000. You should see a web page welcoming you aboard Rails.

Creating a Database click here to hide or show section

The following database servers are currently supported by Rails: MySQL, PostgreSQL, SQLite (SQLite3 is the default), SQL Server, DB2, Firebird, and Oracle.

Highslide JS
TextMate project with db config/database.yml in edit window
$ rake db:create:all

db:create:all Create all the local databases defined in config/database.yml (above )

rake Command
  • db:charset Retrieves the charset for the current environment’s database
  • db:collation Retrieves the collation for the current environment’s database
  • db:create Create the database defined in config/database.yml for the current RAILS_ENV
  • db:create:all Create all the local databases defined in config/database.yml
  • db:drop Drops the database for the current RAILS_ENV
  • db:drop:all Drops all the local databases defined in config/database.yml
  • db:reset Drops and recreates the database from db/schema.rb for the current environment.
  • db:rollback Rolls the schema back to the previous version. Specify the number of steps with STEP=n
  • db:version Retrieves the current schema version number

Event Scaffold click here to hide or show section

$ cd expenses $ script/generate scaffold event name:string budget:decimal

Rails 2.0 is REST (Representational State Transfer) by default. The usual suspects are created: Controller, Helper, Model, Migration, Unit Test, Functional Test

The scaffold command contains the database "event" and the columns that added. The columns can also be added by hand.

Database Migration click here to hide or show section

$ rake db:migrate

Rails applications use Rake (Ruby's equivalent of Make) to automate recurring tasks such as applying migrations. We could run rake db:migrate from a Terminal command line, but again the Organizer makes this easier. All the Rake tasks are already available as pre-configured actions.

Highslide JS
Results of the action in terminal window
<$ script/server

Now point your web browser at http://localhost:3000/events

Vendor Scaffold click here to hide or show section

$ script/generate scaffold vendor name:string email:string
$ db:migrate

Expenses Resource click here to hide or show section

$ script/generate resource expense event_id:integer vendor_id:integer amount:decimal
$ db:migrate

Declare the associations in the models as follows:

class Expense < ActiveRecord::Base
  belongs_to :event
  belongs_to :vendor
end
class Event < ActiveRecord::Base
  validates_presence_of :name
  validates_numericality_of :budget, :greater_than => 0.0

  has_many :expenses
  has_many :vendors, :through => :expenses
end
class Vendor < ActiveRecord::Base
  has_many :expenses
  has_many :events, :through => :expenses
end

Search & Talk

↓ expand sections collapse sections ↓

Quick Links showhide

Ukulele showhide

Guitar showhide

Lessons showhide

Instruments showhide

Luthiers showhide

Musicians showhide

Misc. Music showhide

Random Stuff showhide

↓ expand sections collapse sections ↓

Top 50 Ukulele Sites

Featured Books


Website brought to you by
Curt Sheller

Curt Sheller Publications
www.CurtSheller.com
Hosted by MacHighway

Valid CSS!