Trying to remain agile in a BIG design world.

In the midst of my Rails based Web development I was tasked with the creation of a simple, one page Web form. I knew it would be a snap to write given my background in PHP and the broad support for PHP in OS X but I decided to “teach myself” how I could do the same thing in Ruby without the crutch of Rails.

The first step was to create the Web form with static HTML which took about 15 minutes, start to finish. I configured the form action to point to my Ruby script that would simply echo “Thanks!”.

Clicking the “Submit” button on my form produced the “Opening form_handler.rb” window (Windows readers your mileage may vary from this point forward) meaning that Apache did not know what to do with files with “.rb” extensions. A quick visit to /etc/httpd/httpd.conf to add the following line beneath the other AddHandler options did the trick.

AddHandler cgi-script .rb

After restarting Apache, and clicking the “Submit” button again I was greeted with a 500 error. A peek at /var/log/httpd/error_log revealed the following:

Options ExecCGI is off in this directory: /Users/sjobs/Sites/cgi-bin/form_handler.rb

That’s easy enough to fix. Placing a .htaccess file containing the following line resolved that issue:

Options ExecCGI

Finally, with no special modification to Apache included out-of-the-box with OS X except those changes noted above my Ruby CGI script was performing as expected. Total time invested: 30 minutes. Not bad. The basic script, which just chunks out the form values and some other stuff appears below. An excellent reference to Ruby’s CGI library is, of course, located in RDoc.

#!/usr/bin/env ruby
require 'cgi'

cgi = CGI.new("html4")
params = cgi.params

cgi.out() do
  cgi.html() do
    cgi.head{ cgi.title{"TITLE"} } +
    cgi.body() do
      cgi.pre() do
        CGI::escapeHTML(
          "params: " + cgi.params.inspect + "\n" +
          "cookies: " + cgi.cookies.inspect + "\n" +
          ENV.collect() do |key, value|
            key + " --> " + value + "\n"
          end.join("")
        )
      end
    end
  end
end

§6 · April 25, 2007 · Ruby · · [Print]

  • Ajay

    On Panther, I also had to modify the following line in /etc/httpd/users/my_user.conf:

    AllowOverride None
    was changed to
    AllowOverride Options

  • Mike

    I just stumbled upon this blog and was able to replicate your example. However, I have been unsuccessful trying to communicate with SQLite3 with Ruby. It seems to fail at the ‘require “sqlite3″‘ line, though I can execute this on my computer from the command line without any problems. I have also tried directly in the HTML with eruby, but that also failed. Have any advice?

  • https://www.budgetsketch.com/ Bill Barnett

    I assume that you have the sqlite3 gem installed since your script runs as expected via the command line. Is it installed globally (so that the user which runs your HTTP server can access it) or locally? That’s my primary suspicion. Also, what is your HTTP server log telling you?

    Have you seen gist.github.com? Could you paste your code there and share it here? Also, it would be helpful if you could paste any relevant log out into another file using the “Add another file…” link on the gist page.

    Good luck.

  • Mike

    Bill, thanks for replying. After spending a few hours trying various methods, I ended up installing rvm (and reading up on the Apache config files multiple times) and was able to get it working that way. The only modification to the above file that I had made when it was broken was the require ‘sqlite3′ line. 

  • http://pulse.yahoo.com/_FGAJNPSWLYM7JENAU4SSZ3HUZA Guerino

    Hi,

    I’m completely new to all of this and I apologize if my questions seem ignorant.  I tried to create a simple index.html file with a form in my “/Users/guerino1/Sites” directory.  The index.html file body has the simple code…

    This is my header

    Username:

    In the directory “/Users/guerino1/Sites/cgi-bin” I added the “form_handler.rb” file that is in this blog’s example.
    I also added the file “.htaccess” in the “/Users/guerino1/Sites” directory, with the line”Options ExecCGI”.

    When I run the example, I get an error that states: “You don’t have permission to access /~guerino1/cgi-bin/form_handler.rb
    on this server.”

    The URL reads: “http://127.0.0.1/~guerino1/cgi-bin/form_handler.rb?user=myName”

    Does anyone have any idea as to how I can fix this?

    Thanks,

    Frank

  • https://www.budgetsketch.com/ Bill Barnett

    Can you post the relevant parts of /var/log/httpd/error_log?

  • http://pulse.yahoo.com/_FGAJNPSWLYM7JENAU4SSZ3HUZA Guerino

    Hi Bill,

    Thanks for the reply.  I actually got it to work (at least partially).

    I’m working on a Mac and I noticed that if I install the program in the http://localhost area, it works fine.  However, if I install it in the http://localhost/~guerino1 area, it doesn’t run.

    I’m now trying to understand what needs to be changed/configured to allow Apache to execute scripts out of my personal working area.

    My Best,

    Frank