這是我在網上看到的關於如何設置cookie的例子。CGI cookie如何在Ruby中工作?
require "cgi"
cookie = CGI::Cookie.new("rubyweb", "CustID=123", "Part=ABC");
cgi = CGI.new("html3")
cgi.out("cookie" => [cookie]){
cgi.html{
"\nHTML content here"
}
}
我試過這樣做,它設置了cookie,然後出現一個空白頁。
#!/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
所以我的問題首先是,我錯過了什麼/做錯了什麼?其次,我想知道是否有人想解釋.out做的和.header相反,還是有區別?
感謝,
列維
從閱讀中我發現cgi.out可以處理cgi.header的大部分功能。所以這只是一個更簡潔的方式來控制輸出? – Levi 2009-04-12 04:10:51