Skip to content

Instantly share code, notes, and snippets.

@Auraelius
Created May 30, 2014 14:33
Show Gist options
  • Save Auraelius/a8cdcaec8245acba88cf to your computer and use it in GitHub Desktop.
Save Auraelius/a8cdcaec8245acba88cf to your computer and use it in GitHub Desktop.
PCC Sintra demo without comments
require 'sinatra'
require 'data_mapper'
DataMapper.setup(:default, "sqlite3://#{settings.root}/dictionary.sqlite3")
set :port, 4000
set :bind, '0.0.0.0'
class Entry
include DataMapper::Resource
property :id, Serial
property :word, String
property :definition, Text
property :created_at, DateTime
end
DataMapper.finalize
Entry.auto_upgrade!
get '/' do
redirect 'entries'
end
post '/entries' do
e = Entry.new(params[:entry])
e.word.capitalize!
e.save
redirect 'entries'
end
get '/entries' do
@entries = Entry.all(:order => [ :id.desc ], :limit => 20)
erb :entries
end
__END__
@@entries
<!DOCTYPE HTML>
<html>
<head>
<title>Dictionary</title>
<style>
dt {
font-weight: bold;
font-size: large;
}
</style>
</head>
<body>
<h1>Dictionary</h1>
<dl>
<% @entries.each do |e| %>
<dt><%= e.word %></dt>
<dd><%= e.definition %></dd>
<% end %>
</dl>
<h1>Add a word to the dictionary</h1>
<form method="post" action="/entries">
<input type="text" name="entry[word]" value="word"> <br>
<textarea rows="4" cols="50" name="entry[definition]" value="definition">definition</textarea>
<input type="submit" value="Add a word">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment