Rails vs. Grails vs. Helma: The JVM Web Framework Smackdown, Part 1

It's the ultimate JVM Web Framework Smackdown! Three will enter the cage. Only one will exit the victor. Will it be JRuby on Rails? Groovy on Grails? Or Helma?

Rails Views

Rails uses .erb files for its views; a .erb contains HTML with embedded Ruby code, a format similar to scriptlets in JSPs. <%=…%> outputs a value, whereas <%…%> does not. The latter is largely used for control structures.

The view layer is both a strength and weakness of Rails. It is easy to use, and since there is no alternative to embedded code, you need not feel particularly guilty about its use. With the clean structure that Rails offers, code embedded in the view tends to be view-related code.

Nonetheless, ugly is still ugly. Embedded code makes the views awkward for web designers, and there is a constant risk of non-view-related code seeping in where it does not belong. Of all the frameworks, Rails’s views end up the poorest looking.

On the other hand, Rails does deserve credit for a couple of features, most notably partial rendering (nicknamed “partials”) and layouts. Partial rendering allows a developer to reuse an HTML chunk that reoccurs in different pages. (While it is a nice feature, it’s not needed in this application.) Layouts, in contrast, are used heavily in almost every Rails project.

For those not familiar with layouts, the concept is simple. For many applications, you may have a standard header and footer, possibly even a left or right rail that you wish to be shared across all your pages. There are other approaches, but none seems to solve this issue as cleanly as Rails’s layouts. Here is a sample for PenguinMusic found in app/views/layouts/main.html.erb:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <title><%= @title || "PenguinMusic" %></title>
  <%= stylesheet_link_tag 'main' %>
  <%= stylesheet_link_tag 'scaffold' %>
<div id="outer">
  <div id="header">
    <div id="toolbar">
        <%= link_to "Home", :controller=>'songs', :action=>'popular', :id=>0 %>
    <% if !session[:user] %>
      | <%= link_to "Login", :controller=>'users', :action=>'login', :id=>0 %>
      | <%= link_to "Register", :controller=>'users', :action=>'register', :id=>0 %>
    <% else %>
      | <%= link_to "Logout", :controller=>'users', :action=>'logout', :id=>0 %>
    <% end %>
      | <%= link_to "FAQ", :controller=>'misc', :action=>'faq' %>
  <hr />
  <p style="color: green"><%= flash[:notice] %></p>
  <div id="body">
    <%= yield %>


The <%= yield %> towards the bottom of the document is where page-specific content is collected and emitted. You might also be wondering about the <%= flash[:notice] %>. The flash is a hash whose contents are available for the next request in the session. After that, it is automatically emptied. This makes it an ideal tool to display messages to the user.

To use this layout, you must modify the controllers, the final piece of the MVC architecture.


For a Rails application, there is a central controller for the application itself. However, you generally do not make many modifications to it. Instead, you create different controllers for different pieces of your application. For the most part, each controller correlates to a model. If you generated scaffolding for each model in our music store application, you should now have seven controllers. Let’s generate one more:

$ jruby script/generate controller misc

This generates a new MiscController object, which will be useful for adding in actions that don’t fit in neatly elsewhere. For example, many applications have an FAQ page;
the music site does as well. To add the FAQ page, let’s add an action to the MiscController and a new .erb file to the collection of views. The .erb file is standard HTML. The controller is therefore not much more complicated. Here it is in its entirety:

class MiscController < ApplicationController
  layout "main"
  def faq

As mentioned before, the layout to use must be named within the controller. The line layout "main" accomplishes this. With no more work, the FAQ page now has the same header as the rest of the application.

Of course, this controller is not terribly interesting. How does Rails deal with a more complex piece of code? Let’s examine the controller needed for the “most popular songs” page.

class SongsController < ApplicationController
  layout "main"
  def popular
    @genres = Genre.all
    @songs = Song.all :order=>"num_downloads"
    @genre_id = params[:genre]
    if (@genre_id)
      @new_songs = []
      @songs.each do |song|
        if song.album.genre.id.to_s == @genre_id
          @new_songs.push song
      @songs = @new_songs
    @num_songs = @songs.size
    @start_ind = (params[:start] || 0).to_i
    @page_size = (params[:size] || 20).to_i
    @songs = @songs.slice @start_ind, @page_size
    @featured = []
    ["hipcitycruz", "namelyus", "markgrowden"].each do |code|
      @featured.push Album.find_by_public_id code

The popular() method generates the various collections required to render the page. (It also sets up pagination, which is discussed momentarily.) Since the method name is “popular”, Rails looks for the corresponding view in app/views/songs/popular.html.erb.

<div id="left_rail">
  <% @featured.each do |album%>
    <div class="featured-album">
      <em><%= album.artist.name %></em><br />
      <strong><%= album.title %></strong><br />
      <% link_to image_tag("/albumArt/" + album.album_cover_name),
          :controller=>'album', :action=>'display', :id=>album.id %>
      <a href="/albums/display/<%= album.id %>">
        <img width="150" height="150" src="/albumArt/<%= album.album_cover_name %>" />
      <br /><br />
  <% end %>
<div id='genres'>
  <% @genres.each do |genre%>
  <div class="genre-link<%= ' genre-selected' if @genre_id == genre.id.to_s %>">
    <%= link_to genre.name,
        {:controller=>'songs', :action=>'popular', :id=>0, :genre=>genre.id} %>
  <% end %>
  <div class="genre-link<%= ' genre-selected' unless @genre_id %>">
    <%= link_to "All genres",
        {:controller=>'songs', :action=>'popular', :id=>0} %>
<br /><br />
<div id="most_popular">
  <h1>Most Popular</h1>
  <table border="0" cellspacing="5" cellpadding="5">
    <% @songs.each_with_index do |song,i%>
    <tr class="<%= if(i%2 == 0) then "odd-row" else "even-row" end %>">
      <td><a href="<%= song.playlist_url %>"><%= song.name %></a></td>
        link_to song.album.artist.name,
            :controller=>"artists", :action=>"display", :id=>song.album.artist
      %><!--a href="<%= song.album.url %>"><%= song.album.artist.name %></a--></em></td>
        link_to song.album.title, :controller=>"albums", :action=>"display", :id=>song.album
    <% end %>
  <hr />
  while i<@num_songs do
      if (i!=@start_ind)
        link_to (i/@page_size + 1).to_i, :action=>"popular", :id=>0, :start=>i, :genre=>@genre_id
        (i/@page_size + 1).to_s
    i += @page_size

User Authentication

For another example, consider user authentication. Rails already has some plugins to deal with the problem, but let’s create a solution to explore how the feature typically works. At the end of app/controllers/users_controller.rb, add an authorize() method:


  def authorize
    unless session[:user]
      flash[:notice] = "Please log in to view that page"
      redirect_to :action => "login", :id=>0

authorize() can check that a user is logged in before accessing certain pages. By itself, however, it does nothing. To call the method automatically, you must add a before_filter. This code adds both the filter and the actions needed:

require 'digest/md5'

class UsersController < ApplicationController
  layout "main"

  before_filter :authorize, :except => [:login, :login_user]
  def login
    @user = User.new
  def login_user
    tmp = User.new(params[:user])
    @user = User.find_by_username tmp.username
    if @user && @user.password == Digest::MD5.hexdigest(tmp.username + tmp.password)
      flash[:notice] = "Welcome, #{@user.username}"
      session[:user] = @user
      redirect_to :controller=>'songs', :action=>'popular', :id=>0
      render :action=> 'login', :id=>0

With these additions, any attempt to access a users/* page redirects the user to login, unless the user is already logged in to the application. Of course, in a real example, this would be more complex—different pages would require different levels of access.

The application is not done yet, however. The actions exist, but the view is missing. Let’s look at Rails’s facilities for handling forms.

Form Handling

Forms are one of the nastier bits of Web applications. Rails makes it very easy to handle forms that correspond directly to an object. Here is the view for the login page:

<h2>Login to PenguinMusic</h2>

<% form_for(:user, :url=>{:action=>'login_user'}) do |f%>
  <%= f.error_messages %>

    <%= f.label :username %><br />
    <%= f.text_field :username %>
    <%= f.label :password %><br />
    <%= f.password_field :password %>
    <%= f.submit 'Login' %>
<% end %>

Things get a little more complex for extra fields. For example, a form to register users commonly has a password verification field. This would use a slightly different syntax:

    <%= f.label :password_verification %><br />
    <%= password_field_tag :password_verification, '', :size=>30 %>

The different syntax is a little confusing at first. You do get used to it quickly enough, but it is one of the more awkward stumbling blocks.

Comments on "Rails vs. Grails vs. Helma: The JVM Web Framework Smackdown, Part 1"

My programmer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the expenses. But he’s
tryiong none the less. I’ve been using WordPress on various websites for about a year and
am anxious about switching to another platform. I have heard excellent things about blogengine.net.
Is there a way I can transfer all my wordpress content into it?
Any help would be greatly appreciated!

It is necessary to maintain control of one’s thoughts while
performing almost any trading.

Also visit my blog forex brokers in cyprus (https://www.eventbrite.com/e/best-forex-brokers-tickets-18350646277)

Let us start with what the currency exchange rate is.

It is the pace of which one currency could be exchanged for another

Review my website http://www.panorama.it/magazine/tasse-risparmi-conti-correnti/

A lot more essential, you’ve to master what can cause rates to go along or up and regarding the foreign exchange marketplace.

Feel free to surf to my weblog; http://24h.net.au/lifestyle/meet-the-real-life-popeye-and-his-big-hands/

forex trading platforms [kcraftmc.com] is foreign exchange trading and it’s also the
biggest lively most liquid marketplace on the planet.

Admiring the dedication you put into your blog and detailed information you provide.
It’s awesome to come across a blog every once in a while
that isn’t the same unwanted rehashed information. Great read!
I’ve bookmarked your site and I’m including
your RSS feeds to my Google account.

They can be generally grouped into two sort of strategies are profit maximizing strategies decreasing strategies.

Here is my web-site: forex trading basics (data.adliberum.com)

InstaForex providers are of the good attention for over 2
000 000 forex brokers in pakistan (http://otobe4.tea-nifty.com/) traders all around the planet nowadays.

I’m really enjoying the theme/design of your weblog. Do you ever run into any
internet browser compatibility problems? A handful of my blog readers have complained
about my site not operating correctly in Explorer but looks great in Chrome.
Do you have any ideas to help fix this issue?

Trading – so that you can consider tiny gains, new dealers may place extremely limited requests.

Feel free to surf to my web-site forex brokers in cyprus

I am a Chartered Accountant, stock and forex market broker, business owner and home investor.

Feel free to visit my webpage :: http://pipeclass.com/index.php/en/guestbook-libro-visitas

I’ve chosen to wander along the less- sailed road and ventured
to the sphere of trading the a lot longer-term graphs.

Here is my web site: forex brokers in nigeria (Loretta)

Because period of time the persons at FxKeys and also you have coached me how exactly to have a life trading Forex.

Visit my blog :: http://www.linux-mag.com/id/7190/

Instead of seeking for weeks while risking your hard
earned cash, to crack the forex brokers in kenya (http://prezent-tour.ru/index.php?option=com_k2&view=item&id=101:navostok&con=996/rss/news/index.xml) rule, take a moment and feel.

The key elements forex brokers in india-stock trading is usually to not be
unable to follow rigid trading principles and
having self discipline.

For key providers, how big the party and the waiting time are inversely

my blog … health insurance australia

Insurance providers offering class health insurance quotes-insurance are forbidden legally from denying coverage based on ones own recent health or

Great, thanks for sharing this article.Really thank you! Keep writing.

One of our guests recently advised the following website.

Awsome blog! I am loving it!! Will be back later to read some more. I am taking your feeds also.

Say, you got a nice blog article.Much thanks again. Fantastic.

Thank you for great information I was on the lookout for this info for my mission.

This is one awesome post.Really looking forward to read more. Much obliged.

Check beneath, are some totally unrelated websites to ours, having said that, they may be most trustworthy sources that we use.

This is one awesome blog article. Fantastic.

Although websites we backlink to below are considerably not associated to ours, we really feel they may be truly really worth a go as a result of, so have a look.

Just beneath, are various entirely not connected sites to ours, on the other hand, they’re surely worth going over.

Below you will come across the link to some web sites that we consider you need to visit.

Fantastic blog article.Really thank you! Fantastic.

I am so happy to read this. This is the kind of manual that needs to be given and not the random misinformation that’s at the other blogs. Appreciate your sharing this best doc.

Looking forward to reading more. Great blog.Really thank you! Really Great.

Really informative blog article.Thanks Again. Will read on…

One of our guests just lately encouraged the following website.

wow, awesome article. Really Cool.

“obviously like your web site but you need to check the spelling on quite a few of your posts. Many of them are rife with spelling problems and I in finding it very bothersome to tell the truth on the other hand I will definitely come back again.”

One of our visitors lately suggested the following website.

Sites of interest we’ve a link to.

The data mentioned within the article are a few of the ideal readily available.

Leave a Reply