Created
November 22, 2010 18:53
-
-
Save emmapersky/710435 to your computer and use it in GitHub Desktop.
GDI Ruby. Class 3. DataMapper
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
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