Running Ruby through the browser (without rails)

Hi guys,

I’ve written a simple script to test my understanding of cgi. When I
save it as test.rb and try access it through the browser, the browser
tries to download it. However, when I save it as test.cgi or test.txt,
the browser displays the script (it doesn’t run it).

You can see the script here:

What am I missing?


You’re missing the CGI part of CGI :slight_smile:

You need to tell your web server that when it encounters a Ruby file,
it should execute it instead of just treating it like a file. That
config varies from server to server, so check your web server’s docs.

Other tips:

  • make it executable
  • put the #! on line one, not line 2
  • you probably don’t want to emit the status line (“200 OK”) –
    usually the server itself emits that and you start with the headers
    (or with the body)
  • A