[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

HTML form action calling a Ruby def?

Serg Koren

2/13/2008 9:48:00 PM

Hi,
Does anyone have a solution to calling a Ruby method when someone
clicks on the Submit button of an HTML form (without Rails)? And an
ancillary question, is there a way to catch the form input into Ruby
variables? My understanding is that the action element needs to
invoke the server side logic which is a bother if you want to get form
input and pre-process it.

Thanks!

3 Answers

Phlip

2/13/2008 11:01:00 PM

0

Serg Koren wrote:

> Does anyone have a solution to calling a Ruby method when someone
> clicks on the Submit button of an HTML form (without Rails)? And an
> ancillary question, is there a way to catch the form input into Ruby
> variables? My understanding is that the action element needs to
> invoke the server side logic which is a bother if you want to get form
> input and pre-process it.

You are asking how to respond to a POST message with CGI. That's the primitive
layer under websites, and it typically packs parameters into ENV environmental
variables.

But why not Rails - you just need like 2 or 3 lines for that...

Jim Clark

2/13/2008 11:19:00 PM

0

Serg Koren wrote:
> Hi,
> Does anyone have a solution to calling a Ruby method when someone
> clicks on the Submit button of an HTML form (without Rails)? And an
> ancillary question, is there a way to catch the form input into Ruby
> variables? My understanding is that the action element needs to
> invoke the server side logic which is a bother if you want to get form
> input and pre-process it.
>
> Thanks!
>
You can use Ruby exactly as you would use other languages such as Perl
to do this. For instance, place your Ruby script in the cgi-bin
directory, set up the web server to know that .rb or .cgi maps to ruby
(differs depending on OS and web server), and then write your script.
Set the post method of the form to be the URL of your script. Use the
Ruby CGI library
(http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/...) that handles
a lot of the details for you.

This section I pasted below is from the CGI docs regarding form inputs
and Ruby variables. From this it should be clear that passing form
parameters into Ruby variables is trivial.

-Jim


Parameters

The method params() returns a hash of all parameters in the request as
name/value-list pairs, where the value-list is an Array of one or more
values. The CGI
<http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CG... object
itself also behaves as a hash of parameter names to values, but only
returns a single value (as a String) for each parameter name.

For instance, suppose the request contains the parameter
"favourite_colours" with the multiple values "blue" and "green". The
following behaviour would occur:

cgi.params["favourite_colours"] # => ["blue", "green"]
cgi["favourite_colours"] # => "blue"

If a parameter does not exist, the former method will return an empty
array, the latter an empty string. The simplest way to test for
existence of a parameter is by the has_key? method.







Serg Koren

2/13/2008 11:40:00 PM

0

Um I dont want to call the server. I know I can call the server and
handle it via cgi. I want to capture the input and process it
locally on the web page client PRIOR to a server side send.. Sort of
like javascript. I dont think its doable. I was hoping for magic.
Oh well.


Thx anyway.

Sent from my iPhone

On Feb 13, 2008, at 6:18 PM, Jim Clark <diegoslice@gmail.com> wrote:

> Serg Koren wrote:
>> Hi,
>> Does anyone have a solution to calling a Ruby method when someone
>> clicks on the Submit button of an HTML form (without Rails)? And
>> an ancillary question, is there a way to catch the form input into
>> Ruby variables? My understanding is that the action element needs
>> to invoke the server side logic which is a bother if you want to
>> get form input and pre-process it.
>>
>> Thanks!
>>
> You can use Ruby exactly as you would use other languages such as
> Perl to do this. For instance, place your Ruby script in the cgi-bin
> directory, set up the web server to know that .rb or .cgi maps to
> ruby (differs depending on OS and web server), and then write your
> script. Set the post method of the form to be the URL of your
> script. Use the Ruby CGI library (http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/...
> ) that handles a lot of the details for you.
>
> This section I pasted below is from the CGI docs regarding form
> inputs and Ruby variables. From this it should be clear that passing
> form parameters into Ruby variables is trivial.
>
> -Jim
>
>
> Parameters
>
> The method params() returns a hash of all parameters in the request
> as name/value-list pairs, where the value-list is an Array of one or
> more values. The CGI <http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classe...
> > object itself also behaves as a hash of parameter names to values,
> but only returns a single value (as a String) for each parameter name.
>
> For instance, suppose the request contains the parameter
> "favourite_colours" with the multiple values "blue" and "green". The
> following behaviour would occur:
>
> cgi.params["favourite_colours"] # => ["blue", "green"]
> cgi["favourite_colours"] # => "blue"
>
> If a parameter does not exist, the former method will return an
> empty array, the latter an empty string. The simplest way to test
> for existence of a parameter is by the has_key? method.
>
>
>
>
>
>
>