Eruby

From DreamHost
Jump to: navigation, search

Eruby is a way to embed ruby in HTML.

Setup

First, obtain a copy of eruby. This is most easily done by using SSH and simply copying /usr/bin/eruby. Name the file eruby.cgi and stick it in the root of the site you'd like to enable ruby on.

 cp /usr/bin/eruby ~/mysite.com/eruby.cgi

Next, create or edit an .htaccess file. There are two important things to do: make sure .rhtml files get parsed by eruby and make sure apache knows to look for an index.rhtml.

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

Testing

You can use the following simple test to make sure everything is working.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <title>Eruby!</title>
 </head>
 <body>
  <h1>A Test of Eruby</h1>
  <em>The current time should be printed below.</em>
  <p><% print Time.now %></p>
 </body>
</html>

Encoding issues

Note that the compiled in default charset of the eruby program is the iso-8859-1 charset. The charset header produced by eruby is independent of the content of the page served. All pages served by the eruby filter must be of the same charset, preferably UTF-8, or mismatched charset headers will be produced. The compiled in program default will very likely be changed to UTF-8 in a future version of the program but at the time of this writing is still iso-8859-1.

There are two solutions.

  • Compile eruby with the configure.rb --with-charset=UTF-8 option to change the default charset.
  • Override the default charset with the eruby -C UTF-8 command line option.

It is probably easiest is to override the default character set with the command line option. It avoids needing to use a customized eruby program. It is self-documenting as to the behavior. Instead of copying the eruby program binary as described above to eruby.cgi create a shell script wrapper eruby.cgi that calls the eruby binary with the -C UTF-8 option.

 #!/bin/sh
 cd $(dirname ${PATH_TRANSLATED:-/})
 exec /usr/bin/eruby -C UTF-8 "$@"
  • Path seems to be /usr/bin/eruby, but was previously noted as /usr/local/bin/eruby. It is possible this is different by server. Perhaps admin could add a symlink in order to avoid confusion.

This has two advantages. One is that the eruby binary is in the expected system location and can be updated there normally when other system upgrades occur. This example illustrated a location in /usr/local/bin/eruby which is typical for locally compiled and installed programs but your site may use /usr/bin/eruby. Use the appropriate path there. A second advantage is that the working directory in this script is changed to the page directory making this compatible with mod_ruby behavior.

See Also

External Links