Take the 2-minute tour ×

I would like to run inline ruby code embedded in some kind of html webpage. There are ruby servers around like mongrel, thin, but I just wanted to run inline ruby in html without making a so called ruby app conforming to some framework I don't want to learn. I want the ruby code to act like PHP, so that I can access the Database, print out forms and so on.

What setup should I have?

share|improve this question

4 Answers 4

up vote 2 down vote accepted

Admittedly, I have never done this, but mod_ruby should be able to do this. If you are able to install mod_php, you are able to use mod_ruby.

Where embedded PHP looks like this:

    print("Hello world");

embedded Ruby would look like this:

<% var="Hello world %>
<%= var %>

Or something similar; haven't done this for some time ;-) More syntax stuff to be found here. To make a long story short: install mod_ruby and you should be good to go.

share|improve this answer
For people who want to install Mod_ruby, eruby onto apache under Ubuntu, visit this link: ubuntuforums.org/archive/index.php/t-356350.html. The info is a bit dated, but it still works! Hope it helps :) –  chutsu Aug 17 '09 at 12:53

You could take a look at my answer to a similar question on SO. It's a bit similar to Daniel's answer, but I've created a ruby-cgi script that does some "magic" for you.

share|improve this answer

Sinatra is minimal enough to stay out of your way (and not tell you what to do), but fully featured enough that you can get going quickly and without much effort.

You get routes, views (Erb or HAML/SaSS), helpers, filters and support for various database adapters for free. Sinatra doesn't make any assumptions about what you'll be doing. Your project can be as simple or as complex as necessary. More importantly, the documentation is excellent and there are good examples (of real projects!).

Sinatra applications are easy to deploy using any rack compliant HTTP server such as Mongrel or Thin.

share|improve this answer
Not too sure I can embed code within html with sinatra –  chutsu Aug 17 '09 at 12:20
You certainly can –  Tate Johnson Aug 17 '09 at 13:38

Two other options is using either erb and cgi(2) or eruby(3) (if you're having trouble installing mod_ruby):

  1. erb with a *.rb-controller-sort-of. This requires two files for a page, but makes for some simple separation of logic and view (without a "framework").

    It's really easy to setup, just one line in httpd.conf or .htaccess (given executable permissions):

    AddHandler cgi-script .rb

    Your mypage.eruby is a php-like ERB-template:

    <p>Hello <%= foo %>!</p>

    But it involves some coding to use. In your mypage.rb file:

    require 'cgi'
    require 'erb'
    # Use cgi for handling params and sending headers etc.
    cgi = CGI.new
    # connect to db etc.
    # prepare variables (Or rather some object to bind to - read docs for that ...)
    foo = "World"
    # read in your template
    template = ERB.new(File.read('mypage.eruby'))
    content = template.result
    # Print stuff
    print cgi.header
    print content

    Now request mypage.rb

  2. Use the eruby executable (more like classic php-style). Maybe easier for simple scripts.

    Download the eruby executable (there is probably a binary for your platform) and copy it to /cgi-bin/eruby

    In httpd.conf:

    AddType application/x-httpd-eruby .rhtml

    Then create your mypage.rhtml:

        # Do ruby stuff here, connect to DB etc.
        foo = "World"

    Now request mypage.rhtml

I prefer 1, but 2 might feel easier to begin with once eruby is installed. Please excuse me for any errors - I haven't tested my suggested config and code.

(I'm to new to post working links. I'll appreciate if someone with permissions like to fix them.)

share|improve this answer

Your Answer


