ruby-specific CGI question (I think)

K

Kurt M. Dresner

I'm using sessions and forms in my cgi script.

Unfortunately, for some reason, cgi.form seems to be putting some funny
stuff at the end of each form I make. (I have multiple forms on the
page, and it puts it on every one.) The funny stuff looks like this:

<fieldset><input TYPE=HIDDEN NAME="sessionname"
VALUE="session_id"></fieldset>

First of all: Why is it doing this?

Second of all: How do I make it stop?

-Kurt
 
H

Hal E. Fulton

----- Original Message -----
From: "Kurt M. Dresner" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Monday, July 14, 2003 8:36 PM
Subject: Re: ruby-specific CGI question (I think)

The problem is not that it is using session management. The problem is
that these things are being visually displayed in my web browser as
rectangles. Any idea how to make /that/ stop?

Well... doesn't fieldset draw a box?? I've never
used it, but I've seen documentation that says
it does that.

Why is 'fieldset' used there anyway? Will it work
if you remove it?

Hal
 
H

Hal E. Fulton

----- Original Message -----
From: "Mark J. Reed" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Monday, July 14, 2003 9:00 PM
Subject: Re: ruby-specific CGI question (I think)

Yes, the fieldset is the problem, but I get the impression that Kurt
isn't generating that himself, but rather that CGI::Session is
inserting it for him. It'd help if I could see the code, though.

That's my impression also; but why should the
session lib use that feature?

By 'remove it' I meant either save the HTML and
edit it out and see how it looks (may not be
possible) or simply (temporarily) change the
library source.

Hal
 
K

Kurt M. Dresner

Here's my code folks. Again, I don't profess to have anything that is
at all polished - I am just working something out:

-Kurt

#! /usr/bin/env ruby

require "cgi"
require "cgi/session"
require "setdeck"
require "set"
require "setcollection"
require "setboard"

cgi = CGI.new("html4")
addr = cgi.remote_addr.gsub(/\./,'d')
sess = CGI::Session.new( cgi, "session_key" => "crappyset",
"session_id" => addr,
"prefix" => "setgame.",
"tmpdir" => "/tmp")

if sess["deck"].nil?
deck = SetDeck.new
sets = SetCollection.new
board = SetBoard.new(12)
deck.shuffle!
else
deck = Marshal.load(sess["deck"])
sets = Marshal.load(sess["sets"])
board = Marshal.load(sess["board"])
end



if cgi.has_key?('selections')
tomark = cgi['selections'][0].split(',').collect { |x| x.to_i }
tomark.each do |marked|
if cgi.has_key?('uncheck')
board.deselect(marked)
else
board.select(marked)
end
end
end

if cgi.has_key?('findset')
board.markoneset
end

if cgi.has_key?('getset')
newset = Set.new(board.removeSelected)
if newset.valid?
newset.collect
sets.add(newset)
else
takencards = newset.destroy
board.place(takencards) unless takencards.nil?
end
end


sess["deck"] = Marshal.dump(deck)
sess["sets"] = Marshal.dump(sets)
sess["board"] = Marshal.dump(board)
sess.update


paramstring = "Session name: #{addr}" + cgi.br +
"Parameters: " +
(cgi.keys.collect { |key| key.to_s + ": " +
cgi[key].to_s}).join(cgi.br)

board.place(deck.dealn(board.holes.size))

cgi.out {
cgi.html {
cgi.head {"\n" + cgi.title {"What a crappy implementation of set!"}}
+
cgi.body {"\n" + cgi.pre{board.to_s} +
cgi.br +
cgi.form {
cgi.textarea("selections") +
cgi.br +
cgi.submit("Check Them", "check") +
cgi.submit("Un-Check Them", "uncheck")

} +
cgi.form {cgi.submit("Collect Set", "getset")} +
cgi.form {cgi.submit("Find Set", "findset")} \
+ paramstring
}
}
}
 
R

Rasputin

* Hal E. Fulton said:
----- Original Message -----
From: "Mark J. Reed" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Monday, July 14, 2003 9:00 PM
Subject: Re: ruby-specific CGI question (I think)



That's my impression also; but why should the
session lib use that feature?

By 'remove it' I meant either save the HTML and
edit it out and see how it looks (may not be
possible) or simply (temporarily) change the
library source.

Even easier:

cgi.form() seems to just return a String - see that snippet I sent earlier:


formString = cgi.form(method="get") {
cgi.text_field("key") +
cgi.text_field("value") +
cgi.submit("add")
}

You should be able to work on formString like any other String,
remove any garbage that got in there, then just puts() it to the browser.
 
H

Hal E. Fulton

----- Original Message -----
From: "Rasputin" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Monday, July 14, 2003 9:14 PM
Subject: Re: ruby-specific CGI question (I think)

Even easier:

cgi.form() seems to just return a String - see that snippet I sent earlier:


formString = cgi.form(method="get") {
cgi.text_field("key") +
cgi.text_field("value") +
cgi.submit("add")
}

You should be able to work on formString like any other String,
remove any garbage that got in there, then just puts() it to the browser.

Oh, certainly. I'd have seen that if I had
a little less blood in my caffeine stream.

Hal
 
M

Mark J. Reed

cgi = CGI.new("html4")

Okay, here's what's going on. When you create a CGI::Session,
you pass it a CGI object. The session sets the @output_hidden instance
variable within the CGI query object to so that <input type="hidden">
elements get output at the end of every form.

Except that, for some strange reason known only to the author of cgi.rb,
it uses fieldsets:

if @output_hidden
hidden = @output_hidden.collect{|k,v|
"<INPUT TYPE=HIDDEN NAME=\"#{k}\" VALUE=\"#{v}\">"
}.to_s
if defined? fieldset
body += fieldset{ hidden }
else
body += hidden
end
end

The defined? test is just checking to see if fieldset is available -
which it is if you ask for html4, but not if you ask for html3.

I think this is clearly a bug in cgi.rb - there's no reason to
put a fieldset around these elements just because you can. So
your options are:

1. Modify your local copy of cgi.rb so that it doesn't do that. :)
Great if you have permission to do that, otherwise no help.

2. Ask for html3 instead of html4.
This is a bad idea for other reasons, though. You don't want to
limit yourself to HTML 3, which is years out of date.

3. Create your own modified version of cgi.form within the script that
munges the result.

That last option goes something like this:

def fix_form(cgi, *args, &block)
return cgi.form(*args, &block).gsub(/<\/?fieldset[^>]*/i, '')
end

Then everywhere you currently have

cgi.form { blah . . . }

Just replace it with

fix_form(cgi) { blah . . . }

-Mark
 
M

Mark J. Reed

def fix_form(cgi, *args, &block)
return cgi.form(*args, &block).gsub(/<\/?fieldset[^>]*/i, '')
end

Note, however, that this will remove *all* <fieldset> tags in the
form, so if you actually have legitimate uses for them, this isn't
the way to go. You would need to make a smarter munger in that case.

-Mark
 
M

Mark J. Reed

Typo correction:

def fix_form(cgi, *args, &block)
return cgi.form(*args, &block).gsub(/<\/?fieldset[^>]*/i, '')
end

Left off the final >, which is no good. :)

def fix_form(cgi, *args, &block)
Note, however, that this will remove *all* <fieldset> tags in the
form, so if you actually have legitimate uses for them, this isn't
the way to go. You would need to make a smarter munger in that case.

-Mark
 
K

Kurt M. Dresner

I think this is clearly a bug in cgi.rb - there's no reason to
put a fieldset around these elements just because you can. So
your options are:

So now the question is, "How do we get this fixed in the real thing?"

-Kurt "doesn't know about these kinds of things"
 
R

Rasputin

* Hal E. Fulton said:
----- Original Message -----
From: "Rasputin" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Monday, July 14, 2003 9:14 PM
Subject: Re: ruby-specific CGI question (I think)



Oh, certainly. I'd have seen that if I had
a little less blood in my caffeine stream.

:)

I'd never have seen it in a million years if cgi/session.rb didn't
have lots of head-shaped dents in it after me spending all night
trying to get sessions in .rhtml files working....
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top