Ruby CGI + jquery -第一个HelloWorld

今天用Ruby CGI写了第一个小程序,其中也尝试了一下传说中的Jquery,感觉确实不错.其中有一些小技巧,为了不忘记特记录于此.
第一个文件(bbs.cgi):

#!/ruby/bin/ruby -Ks
require "cgi"

bbsfile = open("bbs.dat","r")
mes = CGI.escapeHTML(bbsfile.read)
bbsfile.close

print "Content-type:text/html\n\n"

print <<EOF
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=Shift_JIS">
<script src="jquery.js" type="text/javascript"></script>
<title>掲示板</title>
<script type="text/javascript">
$(document).ready(function(){
$("input[name='s']").click(function(){
if($("input[name='name']").val()==""){
alert("please input your name!");
$("input[name='name']").css({backgroundColor:"yellow",fontWeight:"bolder",border:"3px solid red"});
return false;
}
else if($("input[name='text']").val()==""){
alert("please input message!");
$("input[name='text']").css({backgroundColor:"yellow",fontWeight:"bolder",border:"3px solid red"});
return false;
}
});
$("input[name='name']").keyup(function(){
$("input[name='name']").css({backgroundColor:"",fontWeight:"",border:""});
});
$("input[name='text']").keyup(function(){
$("input[name='text']").css();
});
$("input[type='reset']").click(function(){
$("input[name='name']").css();
$("input[name='text']").css();
});
$("a").click(function(){
$("pre").slideToggle("fast");
});
});
</script>
</head>
<body>
EOF

print <<EOF
<form action="./up.cgi" method="get">
</center>
<h2>簡単な掲示板</h2>
メッセージをどうぞ<br><br>
<table border=0>
<tr><td>メッセージ:<td><input name="text" value="" size=50></tr>
<tr><td>お名前:<td><input name="name" value=""></tr>
<tr><td></td><td><input type="submit" name="s" value="書き込む">
<input type="reset" value="クリア"></tr>
</table>
</form>
<hr>
EOF

print <<EOF
<!--<input type="button" name="msg" value="Message">-->
<a href="#" style="text-decoration:none">>>Message</a>
<pre>
<del>
#{mes}
</del>
</pre>
EOF

print <<EOF
</body>
</html>
EOF

第二个文件(up.cgi):

#!/ruby/bin/ruby -Ks
require "cgi"

cgi=CGI.new
message=cgi["text"]
name=cgi["name"]

bbsfile=open("bbs.dat","a")
bbsfile.write(name + ": " +message+"\n")
bbsfile.close

print "Content-type:text/html\n\n"

print <<EOF
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=shift_JIS">
<title>form</title>
</head>

<body>
書き込みありがとうございました。<br><br>
<a href="bbs.cgi">return></a>
</body>
</html>
EOF