Chapitre VII : Les CGI
<%
require 'cgi'
c = CGI.new( 'text/html' )
# Vérification si une nouvelle peut être ajoutée
if c.has_key?('nouvelle') then
f = File.new( "blog-data.html", "a")
# Ajouter la nouvelle dans un paragraphe
f.puts "<p>#{c[ 'nouvelle' ]}</p>"
begin
f.close
end
end
mode = c[ 'mode' ]
# Afficher les infos
def afficher_info()
if File.exist?("blog-data.html") then
f = File.new( "blog-data.html" )
begin
content = f.readlines
ensure
f.close
end
puts content
else
puts "Pas d'entrée"
end
end
%>
<html>
<body>
<h1>Mon Blog en Ruby</h1>
<hr>
<%
# Afficher les nouvelles du blog
afficher_info()
%>
<hr>
<%
# Affichage du formulaire d'ajout de la nouvelle
if mode == "ajout" then
%>
<form action="blog.rhtml" method="post">
<textarea name="nouvelle">
</textarea>
<br><input type="submit">
</form>
<%
else
%>
<a href="blog.rhtml?mode=ajout">Ajouter une nouvelle</a>
<%
end
%>
</body>
</html>