Ramaze In Ruby In Steel
Rails may be the most famous Ruby development framework - but it certainly isn’t the only one. A new framework called Ramaze has been generating some interest lately. Ramaze claims to have some technical advantages over Rails (such as being faster and thread safe). As a newcomer to Ramaze, however, the thing that really strikes me is how much simpler it is to use. Rails is a vast framework with lots of hidden ‘black magic’ which - even for long time users - can be quite confusing. Moreover, a basic Rails application contains a great many files and folders. A basic Ramaze application, by contrast, is quite small.
Anyway, the best way to get a feel for Ramaze is to use it. There are some pretty good tutorials on the Ramaze web site. The only trouble, from the point of view of a Ruby in Steel user, is that these assume that you will be using nothing more than a system prompt and a simple text editor. Using Ruby In Steel, it is possible to manage Ramaze applications using our code editors, an integrated web browser, project management and debugging tools.
This tutorial explains how to do this. It is based on the first five steps of the ‘ToDo List’ tutorial on the Ramaze site. I strongly recommend that you also read the original tutorial as this contains more in-depth explanations of how Ramaze works and it has extra steps to create a more complete application. For ease of comparison, I have numbered the steps below to match those of the original tutorial.
This tutorial may be used with either Ruby In Steel Text or Ruby In Steel Developer.
Step Zero: First install...
| To open a system prompt in Windows XP, click the Windows Start button then Run and enter cmd.
In Vista, click the Windows Start button and enter cmd into the Search field.
Note that, when you have a Ruby project loaded, you can also open a system prompt using the right-click menu on a selected directory in the Solution Explorer.
At a Windows system prompt enter:
gem install ramaze
This will install the Ramaze framework.
gem install win32console
This installs a package which is expected by Ramaze (you will get a warning if using Ramaze without it); this package colours the server output displayed in the console.
First Step, Create
Create a directory for your Ramaze applications and change to that directory.
At the system prompt, enter:
Now create the basic skeleton of your new ‘Todolist’ application.
At the system prompt, enter:
ramaze --create todolist
Now, in Ruby In Steel, create a solution for your Ramaze project:
Select: File, New Project
Choose Empty Ruby Project
Browse to: C:\ramazeapps\
Name the project: todolist (this name must match the name of your Ramaze application)
Uncheck Create Directory For Solution
This creates a new (empty) Ruby In Steel Solution in the top-level directory of the Ramaze todolist application which we created a few moments ago.
In order to populate the Solution with all the files and directories created by Ramaze click the ‘Synchronize’ button above the Solution Explorer (also available on the Ruby menu).
Second Step, M, like Model
Now we are going to deal with the data-handling part (the ‘Model’) of this application. This tutorial does not require a database server. Instead, its data is saved in a text format (YAML).
In the Solution Explorer, right-click \model folder. Select Add->New Item.
Choose Ruby File and give it the name todolist.rb. Click Add.
Copy this code into the new file, todolist.rb, and save it:
Third Step, V, like View
| TIP: The default Ramaze templating system uses an XML format with embedded Ruby code. To apply code colouring, you can open these files in one of the available editors. For example, to open them in the Rails Editor, select Tools->Options then Text Editor->File extension. Enter the xhtml extension in the dialog and select Rails Editor from the drop-down list. Click Apply then OK. Due to the non-standard tags, however (code is embedded between |
Open the file index.xhtml under the \view directory.
Delete the entre contents of this file and replace it with the following:
Note: The code above is slightly different from that shown in the original tutorial since it omits some elements such as the Html, Head and Body tags. This is because these tags are common to all pages in the application and are defined in the page.xhtml file which performs a similar function to a Rails layout template. The remaining ‘view’ templates such as index.xhtml will be placed inside the page template when the application is run.
Time to test out the application.
Back at the system prompt in the C:\ramazeapps\todolist directory enter this command:
Now go back to Ruby In Steel and start up the integrated web browser by pressing CTRL+W, W (if you are using the default shortcuts) or by selecting View->Other Windows->Web Browser. Alternatively, you may start up a standalone browser such as Internet Explorer or Firefox.
In the address bar enter:
All being well, this is what you will see....
Fourth Step, C, like Controller
Now we’ll add some interaction - to let users add new items. First we need to edit the controller.
Open main.rb under the \controller directory.
Delete the default index method and replace it with this:
Switch to index.xhtml. Delete its contents and replace it with the following:
| Note that the above code contains a link |
Fifth Step, getting dynamic
Right-click the \view folder in the Solution Explorer, select Add->New Item->Empty RHTML File. Name it: new.xhtml (be careful that you use the extension xhtml) and click Add.
Paste this code into the empty document and save it:
Now in main.rb in the \controller folder, add the following method:
The code in main.rb should now look like the following (plus some comments which I have deleted in order to show the code more clearly)...
Note: I experienced some problems getting the running application to adopt the changes I’d made at this point. Try refreshing the application (right-click and select Refresh in the browser). If you see a whole series of errors, switch to the console from which you ran Ramaze, terminate the application (press CTRL+C) then run it again by entering:
Now go back to the browser, refresh and, all being well, you should see the TodoList index with the ‘New Task’ link.
Click the ‘New Task’ link. This will take you to the New Task entry page. Try entering something. Then click the Submit Query button.
This will return you to the front page with the new task now added to the list. Carry on adding tasks to fill out the ToDo List.
This completes the first five steps of the Ramaze ToDo List tutorial. You should now be comfortable with adding and editing files using the Solution Explorer, running scripts from the system prompt, viewing the application using the integrated web browser and adding externally generated files to a solution by Synchronizing. If you would like to explore Ramaze further, we suggest that you continue with the original ToDoList tutorial from Step 6 onwards to learn how to delete items, customize templates and format data.
Debugging Ramaze Applications
It’s worth pointing out that you can use the Ruby In Steel Debugger to place breakpoints on code in the Ruby source files and step through your applications, examining variables in the debug windows or hovering over them to ‘drill down’ and expand complex structures. To debug a Ramaze application, load up the file start.rb and, with this as the active file in the editor, simply start the Ruby In Steel debugger by pressing F5.
I’m glad you like Ramaze! I really like your insight into using Ramaze with ’ruby in steel’.
The only thing that irks me is "Ramaze generates a whole load of files and directories when you create a new project". Despite being used in the tutorial, the old time Ramaze users don’t really use `ramaze —create`. Other than the default template directory (configurable), the application layout is completely up to the user. The `ramaze` binary is partly there to help make a little structure for Rails dropouts. :P
For example, the way I recently tested how fast an Ebb Adapter would be as opposed to Thin or Mongrel:
ruby -e ’require "ramaze"; class MainController < Ramaze::Controller; def index; "Hello World"; end; end; Ramaze.start(:port => 7001, :adapter => :ebb)’ &
ab -n 500 -c 2 http://127.0.0.1/
Being able to start Ramaze like that is just awesome, no structure, no files, no folders to keep in mind, only pure ramaze.
Can you recommend a different tutorial that does it the way "the old time Ramaze users" do it? ;-)
I guess I’m like most other newcomers to Ramaze. I’m following the guidance provided by the featured tutorial (I don’t object to the files that are created, incidentally). If there is another tutorial you would recommend I’ll happily give that a go too.
Nope, no tutorial for that. :) The proto directory (created by `ramaze —create`) has few enough files, getting jQuery by default is quite nice too. Still, once you get a little more comfortable with Ramaze, you will (probably) develop your own structure.
For example I create the following in new projects: start.rb, conf/common.rb, conf/debug.rb, app/controller/, app/model/, template/, public/. I have premade .rb files for the configuration, since that is allmost the same everywhere. (For example I like setting options via environment variables, which my config supports.)
The best ’guidance’ (at least for me) is: When in doubt, just think in Ruby, not Framework. Works perfectly with Ramaze.
This article is very good, but do you think you could expand on it again?
Let’s say in one year or so.
I love the screenshots, but I would have wanted it also to see that you expand your project in the end to become "bigger"
most web apps inevitably will grow as time passes by :)