How to Write Webpages in Ruby without Using Rails

By Hiveminds
Created 2006-12-10 20:57

Ruby on Rails is gaining popularity but what do you do if you want to create web pages in Ruby but don't want Rails? The answer is eRuby [1]. But not many web hosting know about eRuby. If you want eRuby then you will have to them ask for it. I found this out by sending a few emails. Here are the results of the survey.

A week ago I sent out 10 emails to well known web hosting companies. I got the list from the www.rubyonrails.org. In the email I asked a simple question. "Do you support eRuby?" The answers I recieved were varied. Most of the hosting companies sales people and technicians had no idea what eRuby was although they did support Rails and ERB. Some of them answered back that they did not support mod_ruby which is a totally different thing. So I had to go back and explain to them what eruby.cgi was and how to implement it.

After a brief explaination of eRuby and how to get it going I received 6 answers. Of those two hosting companies said that they would install and make available eRuby by request. One did support eRuby already but did not have it listed in available features. This is the explaination [2] that was sent to them, eruby.info [3].

As you may notice we have started a new domain dedicated to the promotion of eRuby. I never thought that it would be necessary but it seems that the noise of Rails is drowning out a greater opportunity for those that are not interested in Rails.

Eruby.info is just starting out but it is hoped that it will be a jumping point for all that want to use the Ruby programming language for web developement. There will be a concentration on three things. First an explaination of what eRuby is and how to get it. Second a list of web hosting companies that support eRuby or will install it on demand. This will require a setup that will allow hosting companies to list themselves. Third part will be a repository of web applications [4] that run without Rails.

By the way those hosting companies that will install eRuby for you are:

A list of hosting companies that have eRuby installed as standard.

There will be more coming as soon as we come up with a letter that both asks and explains eRuby so that we get clear responses and not "we don't support mod_ruby" or "why don't you want to use Rails?" which are the most common answers.

eRuby is not eruby and eruby.cgi is not mod_ruby

eRuby is the implementation of embedding Ruby code in a nother file using <% ruby code %>. eruby with small letters is the CGI binary that does eRuby. mod_ruby is a complete Ruby core running on Apache. The listed items are three different things. Hosting companies don't seem to really understand what mod_ruby is either. mod_ruby embeds the Ruby interpreter into the Apache web server, allowing Ruby CGI scripts to be executed natively. These scripts will start up much faster than without mod_ruby. This is the same as mod_perl or mod_php.

Once all the misunderstandings are cleared and eruby.cgi made available to you then you can get started doing some Ruby web pages. Hosting companies benefit from this because gives a really easy to learn alternative to PHP without them having to install the Ruby on Rails framework.

Forms and eRuby

Echoing back text is nice, but what about multi-step forms that are included in the same .rhtml file? eRuby handles this in the way one would expect. if/else/unless/elsif sections can span eRuby tags. Take the following simple example:

Example 1. if/else eRuby example


<% require 'cgi' # Require the CGI library
cgi = CGI.new()  # New CGI object
username = cgi.param['username']
%><html><head><title>eRuby Test</title></head>
  <body>
    <h1>Testing eRuby</h1>
    <% if username.empty? # Print out the form asking for the username %>
      <form method="post">
        Please enter your username: <input type="text" name="username"><br>
        <input type="submit" value="Go">
      </form>
    <% else %>
      Welcome <%= username %>!
    <% end %>
  </body>
</html>

On the first page request, the user will be prompted for their username. Upon entering the username and submitting the data, the condition changes, and the contents of the else expression is executed, which, in this case prints out the username.

Behind the Scenes of eRuby

When the Apache handler is called via the .htaccess direction, it does a few things for the developer which make it very easy to use as a dynamic web engine.

DirectoryIndex index.rhtml index.html index.htm
AddHandler rubypage .rhtml
Action rubypage /eruby.cgi

eRuby handles sending out the HTTP headers. If there's a server error (syntax, runtime, database, security, etc.), eRuby will perform an internal sub-request to the appropriate 500 error document.

eRuby actually inverts the code so that the HTML is wrapped in Ruby, then executes the Ruby.

Example 2. Compiled eRuby output before execution


      require 'cgi'   # Require the CGI library
      cgi = CGI.new() # New CGI object
      username = cgi.param['username']
      print "<html><head><title>eRuby Test</title></head>\n"
      print " <body>\n"
      print " <h1>Testing eRuby</h1>\n"
      print " "
      if username.empty? # Print out the form asking for the username
      print " \n"
      print " <form method=\"post\">\n"
      print " Please enter your username: <input type=\"text\" name=\"username\"><br>\n"
      print " <input type=\"submit\" value="Go\">\n"
      print " </form>\n"
      print " "
      print "\n"
      else
      print " Welcome "
      print(( username ))
      print "!\n"
      print " "
      end
      print "\n"
      print " </body>\n"
      print "</html>\n"

Seeing that eRuby just inverts the code, like what JSP does before it compiles them into classes, is useful when thinking about program structure, control, etc. Switch statements and exception blocks are very useful for multi-step forms and worth holding onto in the back of your mind.

Now you can see why eRuby is an important part of the Ruby world and how you can make it work for you without using [11] the Rails framework.


Happy Publishing!

Source URL: http://www.hiveminds.co.uk/content/how-to-write-webpages-in-ruby-without-using-rails.html