Beginners Guide to CGI Programming in Ruby
CGI or Common Gateway Interface was created to server content over HTTP web servers using external scripting languages like Perl, Python, Ruby, or compiled binaries of C,C++, etc. Apache, the most popular web server and also others can be easily configured to run CGI scripts.
In this article we'll be looking at configuring Apache to run CGI scripts and writing simple CGI scripts in Ruby.
In the demo example I'll be using a directory rb-bin you may change this to whatever you like, cgi-bin is very popular and generally pre-configured. Do not forget to make all your scripts in the directory executable.
Add the following to Apache's config file, and then restart Apache.
Unlike PHP, with Ruby you'll need to send your own headers. Headers and content of a HTTP response is separated by 2 newlines. Let's see how to send headers, let's write our first Ruby CGI script.
For handling forms we'll need a Ruby library cgi, fortunately it comes bundled with Ruby. Here's how to read form values.
In the above example we get the name & print it out.
Cookies are accessed and created using the cgi library, so it's pretty simple. Let's try it with some code.
Now you can get started with writing CGI scripts in Ruby. Enjoy.
|All times are GMT +5.5. The time now is 19:55.|