CGI oddity

  • Thread starter Dmitry V. Sabanin
  • Start date
D

Dmitry V. Sabanin

Hi,

I'm trying to use cgi.rb from stdlib, and I have following problem. When I use GET to send data to script,
everything is fine, it's also fine when I use POST to do this, but I can't use both in one script.
I.e., if I have URL: http://localhost/cgi-bin/index.rb?page=guestbook, and, I have a form that sends POST data
to it, then variable "page" doesn't appear in CGI#params hash.
It looks it wouldn't be hard to hack that in, but maybe there's another way?
 
T

ts

D> I.e., if I have URL: http://localhost/cgi-bin/index.rb?page=guestbook,
D> and, I have a form that sends POST data
D> to it, then variable "page" doesn't appear in CGI#params hash.
D> It looks it wouldn't be hard to hack that in, but maybe there's another
D> way?

well, if you have a FORM just add an INPUT tag with the type hidden,
something like

<input type=hidden name=page value=guestbook>

and don't use '?page=guestbook'


Guy Decoux
 
D

Dmitry V. Sabanin

D> I.e., if I have URL: http://localhost/cgi-bin/index.rb?page=guestbook,
D> and, I have a form that sends POST data
D> to it, then variable "page" doesn't appear in CGI#params hash.
D> It looks it wouldn't be hard to hack that in, but maybe there's another
D> way?

well, if you have a FORM just add an INPUT tag with the type hidden,
something like

<input type=hidden name=page value=guestbook>

and don't use '?page=guestbook'
This was just an example, there could be a lot of other query arguments and I'm not sure
if I want to maintain their lists in each form that's used through index.rb
 
D

David Heinemeier Hansson

It looks it wouldn't be hard to hack that in, but maybe there's
another way?

I've had the exact same problem. This is a little part of my solution:

class CGI
def query_parameters
@qp = parse_query_parameters(query_string) if @qp.nil?
@qp
end

def parse_query_parameters(query_string)
parsed_params = {}

query_string.split(/[&;]/).each { |p|
k, v = p.split('=')
parsed_params[CGI.unescape(k)] = v.nil? ? nil : CGI.unescape(v)
}

return parsed_params
end
end

Now you should be able to access all the query_parameters from the hash
returned on that method. Regardless of whether the request was POST or
GET. I doesn't take much effort to include this seemingly with the
original params method, so you don't have to distinguish between GET
and POST variables.

The full CGI extension I did, which also turns
"person[address][street]" style inputs into hashes (like PHP), is
included in Rails. The upcoming web-application framework that
currently is just the mirage available at http://rails.nextangle.com/
but hopefully soon will be a released reality.
 
D

Dmitry V. Sabanin

I've had the exact same problem. This is a little part of my solution:

class CGI
...
end

Now you should be able to access all the query_parameters from the hash
returned on that method. Regardless of whether the request was POST or
GET. I doesn't take much effort to include this seemingly with the
original params method, so you don't have to distinguish between GET
and POST variables.
Thanks, I'll checkout the code and see how can I use it :)
The full CGI extension I did, which also turns
"person[address][street]" style inputs into hashes (like PHP), is
Sounds nice
included in Rails. The upcoming web-application framework that
currently is just the mirage available at http://rails.nextangle.com/
but hopefully soon will be a released reality.
He-he, my problem was related to my yet-to-be-released MuraveyWeb project,
it's a web-framework also :)
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top