Rails Writ Large

Ruby on Rails 1.1 and the paradox of how constraints can lead to greater freedom.

Rails 1.1

The first public release of Ruby on Rails was version 0.5, in July
2004. More than a year later (and with nearly every line of code changed)
Ruby on Rails 1.0 was announced in December 2005. That milestone was
preceded by an intense push of polishing and testing to ensure that it
was a solid release—so you might expect that the Rails Core team has
coasted down the tracks since then, enjoying the phenomenal success of
its software.

You might expect that, but you'd be wrong. In fact, they haven't
slowed down one bit, and the next major release of Rails has just been
announced. It's the biggest release to date, with more than 500 enhancements,
fixes and tweaks. The majority of the 500 changes subtly polishes existing
features, but some of them are superstars that promise to change the way
your applications are developed. I've poured through the change logs to
find the most interesting parts, and they can be lumped into three major
groups: powerful Ajax, richer domain models and easy Web services.

Powerful Ajax

Arguably, the most significant new features in Rails 1.1 redefine the way
Rails handles Ajax. Rails already had top-notch support for creating
Ajax applications—it works by sending small snippets of HTML to a
page to be inserted. Now, it also can return JavaScript to the browser
to be evaluated. That means updating more than one page element in one
step is a snap.

The kicker is that instead of writing the JavaScript by hand,
it can be generated by Rails, using Ruby syntax. That's where RJS,
Ruby-generated JavaScript, comes into play. In addition to .rhtml (Ruby
HTML) templates, you can create .rjs (Ruby JavaScript) ones. In them,
you can write Ruby code that will generate JavaScript code, which is
sent as the result of an Ajax call and evaluated by the browser.

Let's look at an example to see how this can be used. The
on-line store IconBuffet uses RJS for its shopping cart (see
www.iconbuffet.com/products/amsterdam to try it out). When a
product is added to the cart, three separate page elements need to be
updated to reflect the change. Before RJS, that would entail a dozen
lines of JavaScript and multiple round-trips to the server. But now,
it can be accomplished in one pass, with no custom JavaScript.

Figure 1. Add an Item to the Cart

The Add to Cart button uses the standard Ajax link helper, just
like before:

The template is rendered into JavaScript that is sent back to
the browser and evaluated, which updates the three page elements
accordingly. You may be wondering where this page object came
from—it's passed to RJS templates to represent JavaScriptGenerator, and it
has many tricks up its sleeve:

Click "add to cart", then click "remove from cart". Now try to click "add to cart." You can't because the buttons still say "remove." Buttons fail to refresh after removing from cart. Must reload page to see the add to cart button again.

I can't replicate this problem. I tested at 800x600 resolution and though the gray code blocks had horizontal scroll bars none of the body text was outside of the screen.

What browser are you using, what screen resolution and what operating system. It would also help if you would check that you have not changed any of you browser settings so that they override the style sheets on the web site.

Webmaster
Linux Journal

All the new OSs and windowing systems are oriented towards content consumption instead of content production.