Skip to content

Instantly share code, notes, and snippets.

@nakajima
Created July 31, 2008 04:20
Show Gist options
  • Select an option

  • Save nakajima/3401 to your computer and use it in GitHub Desktop.

Select an option

Save nakajima/3401 to your computer and use it in GitHub Desktop.
Ain't Aintablog
%w(rubygems dm-core sinatra erb).each { |lib| require lib }
# Easier on the eyes
class String
def erb_eval(b)
ERB.new(self).result(b)
end
end
# Blogs need posts
class Post
include DataMapper::Resource
property :id, Integer, :serial => true
property :title, String
property :body, Text
end
# Setup stuff that only runs once
configure do
DataMapper.setup(:default, 'sqlite3::memory:')
Post.auto_migrate!
HTML_TEMPLATE = <<-HTML
<html>
<head><title>A test</title></head>
<body>
<h1>A blog!</h1>
<% posts.each do |p| %>
<h3><%= p.title %></h3>
<p><%= p.body %></p>
<hr>
<% end %>
<%= "No posts here." if posts.empty? %>
<form action="/create" method="post">
<p>Name:<br><input name="title"></p>
Body:<br><textarea name="body"></textarea>
<p><input type="submit" value="Create a post"></p>
</form>
</body>
</html>
HTML
end
# The actions
get '/' do
posts = Post.all
HTML_TEMPLATE.erb_eval(binding)
end
post '/create' do
p = Post.new \
:title => params[:title],
:body => params[:body]
p.save
puts p.inspect
redirect '/'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment