2004-11-23 20:04:44 -05:00
|
|
|
#!/usr/local/bin/ruby
|
|
|
|
|
|
|
|
$:.unshift(File.dirname(__FILE__) + "/../lib")
|
|
|
|
|
|
|
|
require "action_controller"
|
|
|
|
|
|
|
|
Post = Struct.new("Post", :title, :body)
|
|
|
|
|
|
|
|
class BlogController < ActionController::Base
|
|
|
|
before_filter :initialize_session_storage
|
|
|
|
|
|
|
|
def index
|
|
|
|
@posts = @session["posts"]
|
|
|
|
|
|
|
|
render_template <<-"EOF"
|
|
|
|
<html><body>
|
2006-08-07 02:11:56 -04:00
|
|
|
<%= flash["alert"] %>
|
2004-11-23 20:04:44 -05:00
|
|
|
<h1>Posts</h1>
|
|
|
|
<% @posts.each do |post| %>
|
|
|
|
<p><b><%= post.title %></b><br /><%= post.body %></p>
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
<h1>Create post</h1>
|
|
|
|
<form action="create">
|
|
|
|
Title: <input type="text" name="post[title]"><br>
|
|
|
|
Body: <textarea name="post[body]"></textarea><br>
|
|
|
|
<input type="submit" value="save">
|
|
|
|
</form>
|
|
|
|
|
|
|
|
</body></html>
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2006-08-07 08:40:14 -04:00
|
|
|
@session["posts"].unshift(Post.new(params[:post][:title], params[:post][:body]))
|
2004-11-23 20:04:44 -05:00
|
|
|
flash["alert"] = "New post added!"
|
|
|
|
redirect_to :action => "index"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def initialize_session_storage
|
|
|
|
@session["posts"] = [] if @session["posts"].nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ActionController::Base.template_root = File.dirname(__FILE__)
|
|
|
|
# ActionController::Base.logger = Logger.new("debug.log") # Remove first comment to turn on logging in current dir
|
|
|
|
|
|
|
|
begin
|
|
|
|
BlogController.process_cgi(CGI.new) if $0 == __FILE__
|
|
|
|
rescue => e
|
|
|
|
CGI.new.out { "#{e.class}: #{e.message}" }
|
2006-08-07 02:11:56 -04:00
|
|
|
end
|