Forum: Ruby Sending form data to a text file

A58ee8b0bc67233e15991ae0a48c7da1?d=identicon&s=25 Will H. (4ltr)
on 2010-01-20 20:31
Hi,
I am having problems trying to get input from a html form and write the
form data to a text file called visitor_log.

Here's the code I have been working on:

login.rb:
#!/usr/local/ruby/bin/ruby
require "cgi"
cgi = CGI.new("html4Tr")
cgi.out{
  cgi.html{
    cgi.head{ "\n"+cgi.title{"Formula 1 Shop - Register"} } +
    cgi.body{ "\n"+
      cgi.form("post", "file.rb"){"\n"+
        cgi.h1 { "Formula 1 Shop" } + "\n"+
        cgi.h2 { "Register" } + "\n"+
        cgi.p { "Username:" } + "\n"+
        cgi.text_field("username") +"\n"+
        cgi.p { "Password:" } + "\n"+
        cgi.password_field("password") +"\n"+
        cgi.p { "Email address:" } + "\n"+
        cgi.text_field("email") +"\n"+
        cgi.p { "Age:" } + "\n"+
        cgi.text_field("age") +"\n"+
        cgi.p { "Gender:" } + "\n"+
        cgi.radio_group("gender", "Male", "Female") +"\n"+
        cgi.br +
        cgi.br +
        cgi.submit
      }
    }
  }
}

file.rb:
#!/usr/local/ruby/bin/ruby
require 'cgi'
cgi = CGI.new

time1 = Time.new
forename = cgi.text_field("username")
password = cgi.password_field("password")
email = cgi.text_field("email")
age = cgi.text_field("age")
gender = cgi.radio_group("gender")

open('../logs/visitor_log', 'a') { |f|
  f.puts forename
  f.puts password
  f.puts email
  f.puts age
  f.puts gender
  f.puts time1.inspect
}
A58ee8b0bc67233e15991ae0a48c7da1?d=identicon&s=25 Will H. (4ltr)
on 2010-01-21 13:16
#!/usr/local/bin/ruby

require 'cgi'
cgi = CGI.new

time1 = Time.new
forename = cgi.params['username'] = ["value"]
password = cgi.params['password'] = ["value"]
email = cgi.params['email'] = ["value"]
age = cgi.params['age'] = ["value"]
gender = cgi.params['gender'] = ["value"]

open('../logs/visitor_log', 'a') { |f|
  f.puts forename
  f.puts password
  f.puts email
  f.puts age
  f.puts gender
  f.puts time1.inspect
}

I have changed the variables to cgi.params, and I have looked at the
ruby documentation but I still don't see how I can get the program to
work.
E57b489b556cee6062a2756653051ce6?d=identicon&s=25 Jeff Peng (Guest)
on 2010-01-21 14:03
(Received via mailing list)
在 2010-01-21四的 21:17 +0900,Will H.写道:
> #!/usr/local/bin/ruby
>
> require 'cgi'
> cgi = CGI.new
>

Hi,

Though I never wrote a ruby CGI, but it's not that hard to write one
with class CGI.
I wrote and put this script into cgi-bin directory:

#!/usr/bin/ruby
require 'cgi'

cgi = CGI.new
user = cgi['username']
pass = cgi['password']

time = Time.now.strftime("%D %T")
cgi.out("text/plain") do
  "Time: #{time}\n" +
  "User: #{user}\n" +
  "Pass: #{pass}"
end

And access it with such url like:
http://127.0.0.1/cgi-bin/test.cgi?username=test&pa...

It just works fine.
You also want to check if webserver has the priviledge to write to the
file you like to create. Check webserver's error_log for any error.

Ruby's CGI document:
http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/

HTH.
A58ee8b0bc67233e15991ae0a48c7da1?d=identicon&s=25 Will H. (4ltr)
on 2010-01-21 14:09
Will H. wrote:
> #!/usr/local/bin/ruby
>
> require 'cgi'
> cgi = CGI.new
>
> time1 = Time.new
> forename = cgi.params['username'] = ["value"]
> password = cgi.params['password'] = ["value"]
> email = cgi.params['email'] = ["value"]
> age = cgi.params['age'] = ["value"]
> gender = cgi.params['gender'] = ["value"]
>
> open('../logs/visitor_log', 'a') { |f|
>   f.puts forename
>   f.puts password
>   f.puts email
>   f.puts age
>   f.puts gender
>   f.puts time1.inspect
> }
>
> I have changed the variables to cgi.params, and I have looked at the
> ruby documentation but I still don't see how I can get the program to
> work.

If anyone has the same problem, I have got the program working
by removing = ["value"] from cgi.params

visitor_log output:
will
Password
will@example.net
22
Male
2010-01-21 12:55:47 +0000
A58ee8b0bc67233e15991ae0a48c7da1?d=identicon&s=25 Will H. (4ltr)
on 2010-01-21 14:36
Jeff Peng wrote:
> I wrote and put this script into cgi-bin directory:
>
> #!/usr/bin/ruby
> require 'cgi'
>
> cgi = CGI.new
> user = cgi['username']
> pass = cgi['password']
>
> time = Time.now.strftime("%D %T")
> cgi.out("text/plain") do
>   "Time: #{time}\n" +
>   "User: #{user}\n" +
>   "Pass: #{pass}"
> end
>
> And access it with such url like:
> http://127.0.0.1/cgi-bin/test.cgi?username=test&pa...

Hi Jeff,
Thanks for the code, you posted just after I solved the problem myself.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.