CGI - problem with calling methods

Hi all,

First post here, I hope it is in the right place. I am new to Ruby,
and I am trying to write a simple cgi script.
I want to execute a method inside a cgi script, but I constantly get
the error:

[Sat Apr 12 12:49:39 2008] [error] [client 62.163.186.111] Premature end
of script headers: /Library/WebServer/CGI-Executables/biblio/main.rb

It doesn’t matter if I just define the method locally in the script,
or if I create a class, and define the method there.
If I comment out the line: Person.new().saveNewPerson(“bla”,
“blabla”), the script properly returns the html with “Done”
If I copy the class and the command calling the class, and put them in
separate script (without cgi) and run that script, the “bla” and
“blabla” are written to the Person file as expected.

#!/usr/bin/ruby

class Person
def saveNewPerson(aFirstName, aLastName)
file = File.open("/Users/somePath/Person.xml", “r+”)
file.puts(aFirstName)
file.puts(aLastName)
end
end

require “cgi”

cgi = CGI.new(“html4”)

#saveNewPerson(cgi.params[“firstName”], cgi.params[“lastName”])
Person.new().saveNewPerson(“bla”, “blabla”)

cgi.out {
cgi.html {
cgi.body {
cgi.p {“Done”}
}
}
}

thanks a lot for your help,

Rob

problem solved, it was a file permissions issue for the Person file. I
guess the webserver runs as a different user.

thanks anyway,

Rob

Rob B. wrote:

problem solved, it was a file permissions issue for the Person file. I
guess the webserver runs as a different user.

thanks anyway,

Rob

Yep. I was doing some tests, and I came to the same conclusion.

7stud – wrote:

Rob B. wrote:

problem solved, it was a file permissions issue for the Person file. I
guess the webserver runs as a different user.

thanks anyway,

Rob

Yep. I was doing some tests, and I came to the same conclusion.

I guess we might as well post the solution for future searchers. To
write to a file from a cgi script,

  1. The folder containing the file needs write and execute persmissions:
    rwx-wx-wx. To change the permissions, first cd to the directory
    containing the folder. Then issue the command:

$ chmod 733 MyFolderContainingTheFileIWantToWriteTo

If you want to read from the file later then, you will need read
permissions as well, so do this:

$ chmod 777 MyFolderContainingTheFileIWantToWriteTo

That sets the permissions to read, write, execute for all users.

  1. The file needs write permissions, e.g. rwxrw-rw-. So cd to the
    directory containing the file and issue the command:

$ chmod 766 FileNameYouWantToWriteTo

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs