#!/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"
<%= @flash["alert"] %>
Posts
<% @posts.each do |post| %>
<%= post.title %>
<%= post.body %>
<% end %>
Create post
EOF
end
def create
@session["posts"].unshift(Post.new(@params["post"]["title"], @params["post"]["body"]))
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}" }
end