Skip to content

Instantly share code, notes, and snippets.

@emmapersky
Created November 22, 2010 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmapersky/710435 to your computer and use it in GitHub Desktop.
Save emmapersky/710435 to your computer and use it in GitHub Desktop.
GDI Ruby. Class 3. DataMapper
require 'rubygems'
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
class Animal
include DataMapper::Resource
property :id, Serial
property :name, String
def about
"#{@name} is urrrrrr?"
end
end
configure do
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite::memory:')
DataMapper.auto_migrate!
end
get '/' do
@animals = Animal.all
erb :index
end
post '/animal' do
animal = Animal.new(:name => params[:name])
animal.save
redirect '/'
end
__END__
@@ index
<html>
<head>
<title>Animals!</title>
</head>
<body>
<h3>Create an animal</h3>
<form method='post' action='/animal'>
<label for='name'>name</label><input type='text' name='name' /><br />
<input type='submit' />
</form>
<hr />
<h3>Listing <%= @animals.size %> Animals</h3>
<ul>
<% @animals.each do |animal| %>
<li><%= animal.about%></li>
<% end %>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment