# HTML form action calling a Ruby def?

**URL:** <https://www.ruby-forum.com/t/html-form-action-calling-a-ruby-def/129065>\
**Category:** Ruby\
**Created:** [February 13, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/html-form-action-calling-a-ruby-def/129065 "2008-02-13T22:48:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serg\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@Serg\_K](https://www.ruby-forum.com/u/Serg_K)\
**Post date:** [February 13, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/html-form-action-calling-a-ruby-def/129065/1 "2008-02-13T22:48:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Serg\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@Serg\_K](https://www.ruby-forum.com/u/Serg_K)\
**Post date:** [February 14, 2008, 12:19am UTC](https://www.ruby-forum.com/t/html-form-action-calling-a-ruby-def/129065/2 "2008-02-14T00:19:58Z")

</div>

Serg K. 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/index.html](http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/index.html)) 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/CGI.html](http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html) 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.

---

<div class="post-metadata">

**Author:** ![Serg\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@Serg\_K](https://www.ruby-forum.com/u/Serg_K)\
**Post date:** [February 14, 2008, 12:41am UTC](https://www.ruby-forum.com/t/html-form-action-calling-a-ruby-def/129065/3 "2008-02-14T00:41:09Z")

</div>

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
