Ruby CGI


#1

Can anyone give me some pointers on how to get a Ruby script running as a CGI script? Essentially I just want to be able to access the script directly from the browser and see its output. I’ve looked all over the place for information on how to do this, but nothing I’ve found has worked; it’s just given me a nice big 500 error with no explanation as to what went wrong. eRuby works fine for me, but I want to know how to get a pure Ruby script to work.

Thanks!


#2

What do your error logs say?

–rlparker


#3

In addition to checking your error logs, try this simple test:

1)Create a text file containing the following text and save it as “hello.cgi”:

#!/usr/bin/ruby
require 'cgi’
CGI::new.out{ “Hello World!\n” }

  1. If you didn’t create it on the server in a webaccessible location, upload it to a web-accessible directory in ascii mode.

  2. Set the permissions to 0755

  3. Browse to that file with a web-browser, as in:

http://yourdomain/hello.cgi

(See it “working” here) :wink:

–rlparker


#4

It worked :expressionless:

I’ve come from a PHP/C# background so I’m not familiar with how Ruby works in a web environment. I was trying to get it to output stuff via ‘print’ but didn’t realise you had to send the content type header first.

The book I was reading (http://www.rubycentral.com/pickaxe/web.html) unhelpfully gave an example that did this but also sent the HTTP response code before that, which stopped it from working again, so I didn’t realise what the problem was.

Is there anywhere I can find a decent guide to web programming in Ruby that doesn’t involve Rails? None of the ones I’ve seen have actually explained details like this (e.g. that you need to send the header first) – things that would be completely non-obvious to a beginner.

Another couple of questions:

  1. How would I get it to display the error messages rather than sending a 500 and just dumping them in the log?
  2. Is there any way of ‘including’ an eRuby file from a normal CGI Ruby script in the way that PHP inclusion works? (i.e. the script is evaluated and all output is sent to the output buffer.)

Anyway, thanks a lot for clearing that up for me! :stuck_out_tongue:


#5

Hey, you are welcome … but unfortunately that just about exhausted my knowledge of Ruby :wink:

I’m betting “tuning” a Google search or two could get you most of the information you need; I’m sorry I don’t know the answers to your other questions.

I just wanted to encourage you to the degree that I could. Maybe some Ruby wizards will jump in and teach us both! :wink:

–rlparker