[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

eRuby and URL rewriting

Orion Hunter

11/15/2003 10:12:00 PM

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?ke...


Does eRuby have the equivalent?

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


6 Answers

Rasputin

11/17/2003 1:09:00 PM

0

* Orion Hunter <orion2480@hotmail.com> [1112 22:12]:
> 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?ke...

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

--
The price of seeking to force our beliefs on others is that someday
they might force their beliefs on us.
-- Mario Cuomo
Rasputin :: Jack of All Trades - Master of Nuns


Rodrigo Bermejo

11/17/2003 3:36:00 PM

0



http://www.xyz.com/index.html?ke...&key...


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.

Rasputin wrote:

>* Orion Hunter <orion2480@hotmail.com> [1112 22:12]:
>
>
>>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?ke...
>>
>>
>
>use CGI.rb for that kind of thing, it plays nicely with eruby.
>
>
>



Eric Schwartz

11/18/2003 12:16:00 AM

0

"Bermejo, Rodrigo" <rodrigo.bermejo@ps.ge.com> writes:
> 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
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.

Rodrigo Bermejo

11/18/2003 12:36:00 AM

0



Eric Schwartz wrote:

>"Bermejo, Rodrigo" <rodrigo.bermejo@ps.ge.com> writes:
>
>
>>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,
>
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.




John W. Long

11/18/2003 4:53:00 AM

0

-- "Bermejo, Rodrigo" <rodrigo.bermejo@ps.ge.com> writes: --
> 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&fru...

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



Rasputin

11/18/2003 2:08:00 PM

0

* Bermejo, Rodrigo <rodrigo.bermejo@ps.ge.com> [1135 15:35]:
>
> http://www.xyz.com/index.html?ke...&key...
>
> 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

> Rasputin wrote:
>
> >* Orion Hunter <orion2480@hotmail.com> [1112 22:12]:
> >
> >>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

> >>given the following URL: http://www.xyz.com/index.html?ke...

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>


--
Radioactive cats have 18 half-lives.
Rasputin :: Jack of All Trades - Master of Nuns