# How to verify cgi.params values?

**URL:** <https://www.ruby-forum.com/t/how-to-verify-cgi-params-values/99783>\
**Category:** Ruby\
**Created:** [July 25, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/how-to-verify-cgi-params-values/99783 "2006-07-25T15:43:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![carl\_mcdade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@carl\_mcdade](https://www.ruby-forum.com/u/carl_mcdade)\
**Post date:** [July 25, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/how-to-verify-cgi-params-values/99783/1 "2006-07-25T15:43:47Z")

</div>

Hi I am running Ruby as CGI and have a form that returns a simple  
string.  
The porblem I am having is that if nothing is entered in the form the  
use of  
.empty does not work. There must be a carriage return or a space left  
behind  
that empty picks up. How do I get remove this?

I have tried

profession = cgi.params[‘profession’].strip

but only get a CGI 500 error.

 Welcome to eruby Test 
## eRuby test
\<% # Require the CGI library require 'cgi'
# New CGI object

cgi = CGI.new

# prepare trap for form variables

username = cgi.params[‘username’]  
profession = cgi.params[‘profession’]  
%\>

```
  <form method="post">
    Please enter your username: <input type="text" 

```

name=“username”\>  
  
Please enter your profession:

```
<% if username.empty? # Print out the form asking for the username 

```

%\>

Please test my first eRuby application by entering your name in  
the  
text box.

  
\<% else %\>  
Thanks, \<%= username %\>!  
  
\<% if profession.empty? %\>  

So you are unemployed? Don’t worry learn eRuby and you  
will  
have a job in no time.

  
\<% else %\>  
\<%= profession %\> sounds like a fun profession.  
\<% end %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![carl\_mcdade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@carl\_mcdade](https://www.ruby-forum.com/u/carl_mcdade)\
**Post date:** [July 25, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/how-to-verify-cgi-params-values/99783/2 "2006-07-25T16:51:44Z")

</div>

Figured it out by trial and error. I was trying to strip an object so I  
datatyped it to a string and then used strip

username = cgi.params[‘username’].to\_s.strip  
profession = cgi.params[‘profession’].to\_s.strip

This works unless there is something else I should use?  
Coming from PHP so this is not easy.

---

<div class="post-metadata">

**Author:** ![carl\_mcdade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@carl\_mcdade](https://www.ruby-forum.com/u/carl_mcdade)\
**Post date:** [July 25, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/how-to-verify-cgi-params-values/99783/3 "2006-07-25T20:40:49Z")

</div>

tesla wrote:

> Figured it out by trial and error. I was trying to strip an object so I  
> datatyped it to a string and then used strip
> 
> username = cgi.params[‘username’].to\_s.strip  
> profession = cgi.params[‘profession’].to\_s.strip
> 
> This works unless there is something else I should use?  
> Coming from PHP so this is not easy.

Yes, use:

username = cgi[‘username’].strip  
profession = cgi[‘profession’].strip

This will return what you are wanting. Using CGI#params[] returns an  
Array for each parameter (in case it has multiple values). CGI#[]  
returns just a single value (the first in the array), which is what you  
want.

It was useful for me to use:

p username  
p profession

to see what was going on. Then read the documentation:

[http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/QueryExtension.html](http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/QueryExtension.html)

Hope that helps.

-Justin

---

<div class="post-metadata">

**Author:** ![carl\_mcdade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@carl\_mcdade](https://www.ruby-forum.com/u/carl_mcdade)\
**Post date:** [July 25, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/how-to-verify-cgi-params-values/99783/4 "2006-07-25T20:40:59Z")

</div>

tesla wrote:

> Figured it out by trial and error. I was trying to strip an object so I  
> datatyped it to a string and then used strip
> 
> username = cgi.params[‘username’].to\_s.strip  
> profession = cgi.params[‘profession’].to\_s.strip
> 
> This works unless there is something else I should use?  
> Coming from PHP so this is not easy.

Yes, use:

username = cgi[‘username’].strip  
profession = cgi[‘profession’].strip

This will return what you are wanting. Using CGI#params[] returns an  
Array for each parameter (in case it has multiple values). CGI#[]  
returns just a single value (the first in the array), which is what you  
want.

It was useful for me to use:

p username  
p profession

to see what was going on. Then read the documentation:

[http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/QueryExtension.html](http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/QueryExtension.html)

Hope that helps.

-Justin
