4

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?

5 Answers 5

3

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:

<?php
    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.

1
1

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.

0

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.

2
  • Not too sure I can embed code within html with sinatra
    – chutsu
    Aug 17, 2009 at 12:20
  • You certainly can Aug 17, 2009 at 13:38
0

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:

    #!/usr/bin/ruby
    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
    Action application/x-httpd-eruby /cgi-bin/eruby
    

    Then create your mypage.rhtml:

    <% 
        # Do ruby stuff here, connect to DB etc.
        foo = "World"
    %>
    <p>Hello <%= foo %>!</p>
    

    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.)

0

A very simple setup is to use Phusion Passenger (a web application server capable of running any Ruby rack application, which works both standalone or as an apache/ngix module) + rack server pages gem (which is a small rack application allowing you to serve simple HTML pages with embedded Ruby code).

Using Passenger in the standalone mode, you only need to run "passenger start" in the root directory of the files you want to serve, and have in that folder, a config.ru file with just 2 lines:

require 'rack-server-pages'
run Rack::ServerPages

You can now serve any .erb files by putting them in the public subfolder.

You could also just run "rackup" and let the pages be served by a ruby server of your choice.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .