RoR Kicks OpenXava in the Butt

In response to Javier Paniza’s article Java Kicks Ruby on Rails in the Butt I thought I’d look at it using actual RoR, rather than the mockery of it made in the article. Javier ignores all the best practices of Ruby and RoR in his code, but (I assume) follows the best practices with his framework of choice, OpenXava.

In the console:

$ rails cookbook
$ cd cookbook
$ script/generate scaffold category name:string
$ script/generate scaffold recipe category:references title:string description:string instructions:text date:date
$ rake db:create
$ rake db:migrate
$ rm public/index.html
$ rm public/images/rails.png

Okay, so the last two aren’t strictly necessary, but I like to keep my app tidy.

Now, add links to switch between categories and recipes:

# app/views/categories/index.html.erb
<%= link_to 'Recipes', recipes_path %>

# app/views/recipes/index.html.erb
<%= link_to 'Categories', categories_path %>

Recipes already belong to categories, but we need to map the relationship in the opposite direction.

# app/models/categories.rb
has_many :recipes

Change the text field for the category to a selection of all available categories.

# app/views/recipes/new.html.erb
<%= :category,{ |c| [,] } %>

Refactor the code for the recipes by copying out the fields from the form (between form_for and the Create button) and paste into app/views/recipes/_form.html.erb, replacing the code in the new and edit templates with

<%= render :partial => 'form', :locals => {:f => f} %>

Repeat for the new and edit templates in app/views/categories.

Then set the default home page to the recipes page.

# config/routes.rb
map.root :controller => 'recipes'

Start the app with script/server and you’re done.

No doubt OpenXava is a powerful framework, and I think I’ll take a look at it. If nothing else, thanks, Javier, for introducing me to a new framework. For starters, it looks a hell of a lot less painful than Struts. But using best practices (which Javier no doubt uses for his Java code), I can generate a fully working app (including code refactoring and other house-keeping) in a dozen lines, with models consisting of a single line of code each. Beat that, Java.


~ by MattR on May 19, 2009.

3 Responses to “RoR Kicks OpenXava in the Butt”

  1. you missed something, he’s comparing rails’ scaffold with an automatic CRUD generator which is unfair, activescaffold could make things even faster than openxava.

  2. I spent some time trying to compare Domain Driven Java Frameworks
    such as OpenXava, Naked Objects, Roma Framework.
    I choose OpenXava to work with.

    Java never die

  3. @javaghost:

    Grails would be my first pick, no questions. It’s what I’m using for my current project (chosen over JRuby for deployment reasons).

    Technically it’s Groovy rather than Java, but you can write it all in Java if you really want.

    The Restlet API was also interesting.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: