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.
<html>
<head>
<title>Welcome to eruby Test</title>
</head>
<body>
<h2>eRuby test</h2>
<%
# 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"><br>
Please enter your profession: <input type="text"
name="profession"><br>
<input type="submit" value="Go">
</form>
<% if username.empty? # Print out the form asking for the username %>
<p>Please test my first eRuby application by entering your name in the
text box.</p>
<% else %>
Thanks, <%= username %>!<br>
<% if profession.empty? %>
<p>So you are unemployed? Don't worry learn eRuby and you will
have a job in no time.</p>
<% else %>
<%= profession %> sounds like a fun profession.
<% end %>
<% end %>
This works unless there is something else I should use?
Coming from PHP so this is not easy.
···
On 7/25/06, carl mcdade <tesla.nicoli@gmail.com> wrote:
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.
<html>
<head>
<title>Welcome to eruby Test</title>
</head>
<body>
<h2>eRuby test</h2>
<%
# 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"><br>
Please enter your profession: <input type="text"
name="profession"><br>
<input type="submit" value="Go">
</form>
<% if username.empty? # Print out the form asking for the username %>
<p>Please test my first eRuby application by entering your name in
the
text box.</p>
<% else %>
Thanks, <%= username %>!<br>
<% if profession.empty? %>
<p>So you are unemployed? Don't worry learn eRuby and you will
have a job in no time.</p>
<% else %>
<%= profession %> sounds like a fun profession.
<% end %>
<% end %>
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:
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: