Created
September 3, 2008 23:26
-
-
Save nakajima/8691 to your computer and use it in GitHub Desktop.
Giving erector a fair shake.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%w(rubygems dm-core dm-validations erector sinatra erb).each { |lib| require lib } | |
class Blog < Erector::Widget | |
def initialize(err=false) | |
@err = err | |
super | |
end | |
def render | |
html do | |
head do | |
title "A blog!" | |
style(:type => 'text/css') { text "label { display: block; }" } | |
end | |
body do | |
h1 "A blog!" | |
p "Couldn't save!" if @err | |
Post.all.each do |this| | |
h3 this.title | |
p this.body | |
end | |
small "no posts here!" if Post.all.empty? | |
form(:action => '/create', :method => 'post') do | |
p label_and_field 'title' | |
p label_and_field 'body', :textarea | |
p { input :type => 'submit', :value => 'Create!' } | |
end | |
end | |
end | |
end | |
def label_and_field(name, tag=:input) | |
label(:for => name) { text "#{name}:" } | |
block_given? ? yield : send(tag, :name => name) | |
end | |
end | |
# Blogs need posts | |
class Post | |
include DataMapper::Resource | |
property :id, Integer, :serial => true | |
property :title, String, :nullable => false | |
property :body, Text, :nullable => false | |
end | |
# Setup stuff that only runs once | |
configure do | |
DataMapper.setup(:default, 'sqlite3:db.sqlite3') | |
Post.auto_migrate! | |
end | |
# The actions | |
get '/' do | |
Blog.new(params[:err]).to_s | |
end | |
post '/create' do | |
p = Post.new params | |
puts p.inspect | |
redirect p.save ? '/' : '/?err=1' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment