Rolling with Flash Rails


home

This is an adaptation of Curt Hibb's popular Rolling with Ruby on Rails tutorial.  This is the article that got me interested in Rails and then non-Rails scripting with Ruby.  I'm not only modifying it to use Flash Rails, but also updating it, doing all database administration with migrations, and using RadRails.  You should read the original article for lots of good information on Ruby and Rails that I am not including here.

Installing the Software

Flash Rails only runs on Windows right now, so like the original article, this will be done on Windows.  We'll only need to install Flash Rails and RadRails.

Install Flash Rails

1.    Download the latest Flash Rails Standard from http://rubyforge.org/projects/flashrails/
2.    Extract the download (either the 7z or zip file) to your desktop (or anywhere you like).

This will give you a folder called 'Flash Rails'.  When I talk about the Flash Rails root folder, this is the folder I'm talking about.

Install RadRails into Flash Rails

1.    Download the RadRails 0.7.2 zip file from http://sourceforge.net/projects/radrails
2.    Extract the download (radrails-0.7.2-win32.zip) into the Flash Rails root folder.

This will put a 'RadRails' folder under the Flash Rails root folder.

Update Path Information

1.    Run 'update_path.exe' in the Flash Rails root folder.

This takes about 2 seconds.  You need to wait to do this until after the RadRails steps above because Flash Rails will not update RadRails information until it either sees a .metadata folder in 'ruby_apps' or in this case it finds the RadRails program under the Flash Rails root folder.

Writing Code

Create the Cookbook Application

1.    Start RadRails by running the 'radrails.exe' directly under the 'RadRails' folder.  The icon looks like this:  RadRails icon
2.    Click 'OK' on the Workspace Launcher window.  The path to the ruby_apps folder should already be filled in.
3.    Right-click in the left RadRails pane and choose 'New' -> 'Rails Project'
New Rails Project

4.    In the project name box, type 'cookbook'.  I would also uncheck 'Create a WEBrick server' and check 'Create a Mongrel server' instead. Then click 'Finish'.
Project details
You could use a WEBrick server if you really wanted to, but I personally use Mongrel in my production environment, so I might as well do testing and development on Mongrel.

After clicking 'Finish', RadRails will switch the bottom pane to the 'console' tab (you may need to wait a second or two) and some text will fly through it.  You can scroll up through this "console" text and read it if you want.  It is the same text that would be displayed if you created your Rails application from the command line.

Test the New Application

1.    Click on the 'Servers' tab in the bottom RadRails pane.
2.    Right-click on 'cookbookServer' and choose 'Start'.  Notice what Port is listed for cookbookServer.
3.    Open your web browser and browse to http://localhost:3000 (the 3000 is the aforementioned Port of the cookbookServer).

It should look like this:
Rails default webpage

Creating the Database

This is where I will diverge drastically from Curt Hibb's original article.  I will show you how to use migrations to create and define your database schema.  This is really nice because you don't need to learn sql or use any other application to create and manage your database.  We will also NOT be editing the database configuration in rails (located in config/database.yml in the application's directory) because the default works great for a development environment.

It is also incredibly convenient since Rails 2.0 uses sqlite by default and stores the database in a file under the application's directory which makes it automatically portable.  Everything you need for Rails to use Sqlite is already included and configured in Flash Rails.  When you deploy your application and move to a production database (such as MySQL), all you have to do is edit the 'production' section in the database.yml, create the empty database (I think Rails will only create Sqlite databases automatically), and re-run your migrations.  Migrations configure your database and abstracts the differences between database servers.

1.    Click on the 'Generators' tab in the RadRails bottom panel.
2.    Choose 'model' in the left drop-down list, and type 'recipe' into the right drop-down box, then click 'Go'
generate recipe model
You'll see some text put into the 'Console' tab and then your migration and your model is created.

3.    In the RadRails left pane, click the plus sign to expand the cookbook application, expand the 'db' folder, then double-click to open the '001_create_recipes.rb' file.
4.    Delete the 't.timestamps', and add the following after the 'create_table :recipes do |t|' line:

t.string "title",        :limit => 100, :default => "", :null => false
t.string "description"
t.date   "date"
t.text   "instructions"

I usually keep the t.timestamps, but to stay consitent with the cookbook application, we'll remove it.  Be sure to save the file.
5.    Click the 'Rake Tasks' tab on the RadRails bottom panel, choose 'db:migrate' in the left drop-down and click 'Go'
recipe migration
Some more text in the 'Console' tab will tell you it's complete (this may take a second or two to get started).

That's it for the database!  These few simple steps got our database created and populated with table information.

Installing ActiveScaffold

Dynamic scaffolding has been deprecated in Rails 2.0.  You can still create static scaffolding, but to stick close to the original Cookbook Tutorial, I'm going to use a plugin called ActiveScaffold.

1.    You'll have to open a command prompt by going to the Flash Rails root folder and running 'use_ruby.cmd'
2.    This leaves you in the ruby_apps folder.  Type 'cd cookbook' to get into the cookbook application folder.
3.    Type 'ruby script\plugin install http://activescaffold.googlecode.com/svn/tags/active_scaffold' to install the ActiveScaffold plugin.

A list of installed files will be displayed and when you are back at a prompt, the install is finished and you can close the command prompt window.

Access Our Data Through a Controller

1.    Go to the Generators tab, choose 'controller' and name it 'recipe' and click 'Go'
2.    In RadRails, open the app\controllers\recipe_controller.rb file and add in 'active_scaffold' so it looks like this:

class RecipeController < ApplicationController
  active_scaffold
end
.

2.    We'll need to also create a View, so in RadRails open the app\views\layouts folder.
3.    Right-click on the layouts folder and choose New > File.  Name the file 'application.rhtml'
4.    Open the file and put this in there:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Generic ActiveScaffold Layout</title>
<%= javascript_include_tag :defaults %>
<%= active_scaffold_includes %>
</head>
<body>
<%= yield %>
</body>
</html>
Save the file and you can now open a web browser and go to http://localhost:3000/recipe and see your new shiny RubyOnRails application.
recipe_page1.png

At this point you should have a working Rails 2.0 application developed with Flash Rails and RadRails.  The original cookbook tutorial expanded on this, but this should give an understanding of how to use Flash Rails.  If you want to move your environment, just copy the Flash Rails root folder (containing RadRails, your applications, everything) to the new location and then re-run the 'update_path.exe'.

Click here to return to the homepage.