From: Mark Hubbart Date: 2004-02-14T07:12:37+09:00 Subject: Re: cgi params in ruby On Feb 13, 2004, at 1:14 PM, Cere Davis wrote: > Hi Rubiers, > > > I can't figure out a way to make the following code do what I want: interestingly enough, I was just working on that same problem this morning :) It looks like there is one error, and one unecessary complication. First, you don't need to convert the cgi.params to an array. You can use it as a hash just fine. Second, the CGI methods like #text_field don't print their result, they return them. So, since you are using Array#each, the value of the statement is... the array itself. Instead, you need to use #map and join the results: > > #!/usr/bin/ruby1.8 -w > > require 'cgi' > > cgi=CGI.new("html3") > cgi.out() do > cgi.html() do > cgi.body() do > cgi.form("get") do > # use Hash#map(), which passes keys and values > cgi.params.map {|key, val| > cgi.text_field(key, val.to_s) + > cgi.br > }.join + # and join the result before moving on > cgi.submit("Okey Dokey?") > > end > end > end > end > > So say I post in something this when I run the script on the command > line: > > > (offline mode: enter name=value pairs on standard input) > lname=davis;fname=cere Note that you get this error here because #each returns an array, and you are trying to add a string to it: > ../t.cgi:15:in `+': cannot convert String into Array (TypeError) > from ./t.cgi:15 > from ./t.cgi:9:in `form' > from ./t.cgi:9 > from ./t.cgi:8:in `body' > from ./t.cgi:8 > from ./t.cgi:7:in `html' > from /usr/lib/ruby/1.8/cgi.rb:1678:in `html' > from /usr/lib/ruby/1.8/cgi.rb:1678:in `html' > from ./t.cgi:7 > from ./t.cgi:6:in `out' > from ./t.cgi:6 > > > What I want to happen is for the cgi script to print the lname and > fname into two text_field boxes but I am having trouble tracing > through this error. Can anyone help? HTH, Mark