eRuby and URL rewriting

O

Orion Hunter

I am in the process of doing some web page authoring using eRuby.

My question is this-- how do I access the post/get information in URL
rewriting? I know in PHP you use something like

$_GET["key"]

given the following URL: http://www.xyz.com/index.html?key=RUBYRULZ


Does eRuby have the equivalent?

_________________________________________________________________
Compare high-speed Internet plans, starting at $26.95.
https://broadband.msn.com (Prices may vary by service area.)
 
B

Bermejo, Rodrigo

http://www.xyz.com/index.html?key=RUBYRULZ&key2=rubyrulz


require 'cgi'
cgi = CGI.new

key = cgi['key']

p key
# [ "RUBYRULZ " ]

key.type
# Array

cgi.query_string.split("&").each {|x| p x }
# "RUBYRULZ "
#"rubyrulz"

-r.
I am in the process of doing some web page authoring using eRuby.

My question is this-- how do I access the post/get information in URL
rewriting? I know in PHP you use something like

$_GET["key"]

given the following URL: http://www.xyz.com/index.html?key=RUBYRULZ

use CGI.rb for that kind of thing, it plays nicely with eruby.
 
E

Eric Schwartz

Bermejo said:
cgi.query_string.split("&").each {|x| p x }
# "RUBYRULZ "
#"rubyrulz"

Actually, it's:

irb(main):002:0> cgi.query_string.split("&").each {|x| p x }
"key=RUBYRULZ"
"key2=rubyrulz"

I'm sure you just meant that as an example of how to access the query
string directly, but FYI, it's broken in other ways too-- modern
browsers are allowed to use ';' to separate variables in a GET
request. In fact, I believe it is now the preferred form. Much
friendlier to do:

cgi.keys.each { |key| p cgi[key] }

anyway :)

-=Eric
 
B

Bermejo, Rodrigo

Eric said:
Actually, it's:

irb(main):002:0> cgi.query_string.split("&").each {|x| p x }
"key=RUBYRULZ"
"key2=rubyrulz"

I'm sure you just meant that as an example of how to access the query
string directly,
Yeah a mistake...I'm not as cleaver as irb =)
but FYI, it's broken in other ways too-- modern
browsers are allowed to use ';' to separate variables in a GET
request.
Oh, I did know it .....modern browsers like ... ? ...just to be aware of
In fact, I believe it is now the preferred form. Much
friendlier to do:

cgi.keys.each { |key| p cgi[key] }
Thanks


anyway :)

-=Eric
BTW, I really don't understand why cgi['query'].class => Array ?

-Ronnie.
 
J

John W. Long

-- "Bermejo said:
BTW, I really don't understand why cgi['query'].class => Array ?

Because it is possible to specify more than one value for param either in
the URL in the case of a GET or in the header in the case of a POST.

For example, given the following form:

<html>
<body>
<form method="get">
What kinds of fruit do you like? <br />
<input type="checkbox" name="fruit" value="apples" /> Apples <br />
<input type="checkbox" name="fruit" value="oranges" /> Oranges <br />
<input type="checkbox" name="fruit" value="cherries"/> Cherries <br
/>
<input type="submit" />
</form>
</body>
</html>

If you check Apples and Oranges, then press submit the following url will be
submitted:

http://myurl.com/?fruit=apples&fruit=Oranges

So:

cgi.params["fruit"] #=> ["apples","oranges"]

If you intend to only allow one value to be submitted, just use Array#first:

fruit = cgi.params["fruit"].first #=> "apples"

I used to think that this was a frustrating syntax, but I now realize this
is an essential part of the HTTP spec.
___________________
John Long
www.wiseheartdesign.com
 
R

Rasputin

* Bermejo said:
http://www.xyz.com/index.html?key=RUBYRULZ&key2=rubyrulz

require 'cgi'
cgi = CGI.new

key = cgi['key']
p key
# [ "RUBYRULZ " ]
key.type
# Array
cgi.query_string.split("&").each {|x| p x }
# "RUBYRULZ "
#"rubyrulz"

See below for an rhtml file

For the sake of completeness, here's a .rhtml example:

<% require 'cgi' # Require the CGI library
cgi = CGI.new() # New CGI object
key = cgi.params['key']
%><html><head><title>Test</title></head>
<body>
<% if key.empty? %>
<form method="post">
Please enter the key: <input type="text" name="key"><br>
<input type="submit" value="go"> </form>
<% else %>
the key is :<%= key %>
<% end %>
</body> </html>
 

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
474,139
Messages
2,570,806
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top