Eruby
From DreamHost
Eruby is a way to embed ruby in HTML.
Contents |
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/local/bin/eruby -C UTF-8 "$@"
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
- www.modruby.net
- www.eruby.info
- eRuby:How to Get Started with Ruby Web Programming
- eRuby:Using Ruby DBI for Database Connectivity
- eRuby:Getting Started with Ruby on Windows IIS
- eRuby: Using Ruby and MySQL for dynamic web pages
- eRuby: Using Embedded Ruby to Create Web Pages
- Erubis (a much faster alternative to ERB and eruby)

