tags:

views:

431

answers:

1

Hey, This is the example I keep seeing online as how to set cookies.

require "cgi"
cookie = CGI::Cookie.new("rubyweb", "CustID=123", "Part=ABC");
cgi = CGI.new("html3")
cgi.out( "cookie" => [cookie] ){
  cgi.html{
    "\nHTML content here"
  }
}

I tried doing it this way and it sets the cookie and then comes up with a blank page.

#!/usr/local/bin/ruby

require 'cgi'
load 'inc_game.cgi'
cgi = CGI.new

cookie = CGI::Cookie.new("rubyweb", "CustID=123", "Part=ABC");
cgi.out( "cookie" => [cookie] ){""}     

#see if game submit buttons pressed
doIt = cgi['play']
puts "Content-type: text/html\n\n"  

play = Game.new

#welcome
if doIt == ''
puts play.displayGreeting
end

#choose weapon
play.playGame

if doIt == 'Play'
    move = cgi['weapon']
    human = play.humanMove(move)
    computer = play.ComputerMove
    print human
    print computer
    result = play.results(human,computer)
    play.displayResults(result)
end

So my question first would be, what am I missing/doing wrong? Secondly I am wondering if anyone would want to explain what .out does as opposed to .header or if there is a difference?

Thanks,

Levi

+1  A: 
Kent Fredric
Sending the header caused an internal server error, this is the option that would work best for me though. Sending the output at the end also causes an internal server error, most likely because I am printing text out in the main body and the print content isn't set until the end
Levi
Consider it a design error at least then. Its really not a good idea ( generally ) to have print statements deep in random code
Kent Fredric
Ok, I will keep that in mind. Do you have any idea as to why the header would throw an internal server error in your first suggestion? I am sending it before anything is printed.
Levi
it depends on your server, you'll want to check your specific servers internal error logs and see its explanation for why it ISE'd, it could be one of dozens of reasons, one being early termination because of invalid code.
Kent Fredric