another silly cgi question

C

Cere Davis

Hello Ruby cgi geniuses,

I cannot figure out how to print html content within the same scope as
the cgi.xx object. That is: When I try and run this:


cgi.out() do
cgi.html() do
cgi.body() do
p userinfo.to_s
cgi.form("get") do

cgi.br +
cgi.params.map {|key, val|
key + " " +
cgi.text_field(key, val) +
cgi.br
}.join +
cgi.br +
cgi.submit("Does this look ok?")
end

end
end
end

I get all the html written before the Content-Type and Content-length
headers get printed...which of course kills my script. If I move the 'p
userinfo.to_s' to after the cgi.submit then the script prints all of
cgi.xx html along with the userinfo data above the Content-Type:
headers. Does anyone know how to shim your own html into the cgi object
so that it will run correctly?

Thanks,
Cere
 
E

Eric Schwartz

Cere Davis said:
Hello Ruby cgi geniuses,

I cannot figure out how to print html content within the same scope as
the cgi.xx object. That is: When I try and run this:


cgi.out() do
cgi.html() do
cgi.body() do
p userinfo.to_s

^^^^^^^^^^^^^^^

That's your problem. I'm not sure what 'userinfo' is here, but the
pickaxe says about Kernel#p:

For each object, directly writes anObject.inspect followed by the
current output record separator to the program's standard output. p
bypasses the Ruby I/O libraries

That means that as soon as you call 'p userinfo.to_s', that gets
printed immediately, instead of as part of your CGI output. This Is
Bad(tm).

The block you pass to CGI#body should return a string, which will be
the contents of the '<body>' '</body>' tags. Instead, you're printing
something to stdout, then building your string below, and after the
string is built, the cgi routines spit out their HTML elements, with
your string inside it.

Also, your indentation is inconsistent and too hard to read at the
start for my taste, and you don't need the () if you're not passing
parameters to the HTML-generating methods. I'd prefer something that
looked like:

cgi.out { # or 'do'..'end', whatever you like
cgi.html {
cgi.body {
userinfo.to_s +
cgi.form('get') { # I only use "" when I'm interpolating variables
cgi.params.collect { |key,val|
key + ' ' + # HTML collapses spaces, so no need to put
cgi.text_field(key,val) + # multiple ones here.
cgi.br
}.join("\n") + # I like "\n" here, as it makes the generated
cgi.br + # HTML easier to read.
cgi.submit("How's this?")
}
}
}
}
Does anyone know how to shim your own html into the cgi object so
that it will run correctly?

I realize that at this very instant, Ruby makes that harder than
necessary, but "Programming Ruby" is a decent reference, and is (most
importantly) free. If you check out the documentation there, you'll
see they provide a better and more complete explanation of cgi.rb than
I have here.

Thanks to Dave Thomas and many others, 'ri' will soon become the
standard reference for Ruby documentation. I can't wait for the day
the default response to any 'what is wrong with this code?' query
becomes, "Did you read the documentation on your hard drive?"

-=Eric
 
C

Cere Davis

Thanks for the critique on my coding style and yes I read the "lack of"
documentation on my hard drive. It's pretty sparse.
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top