CGI - problem with calling methods

R

Rob Boellaard

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
 
R

Rob Boellaard

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

thanks anyway,

Rob
 
7

7stud --

Rob said:
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.
 
7

7stud --

7stud said:
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.



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

$ chmod 766 FileNameYouWantToWriteTo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top