Permalink
Browse files

added a full example for ActiveRecord and HAML

  • Loading branch information...
blindgaenger committed Sep 25, 2009
1 parent 4cbe53b commit 614f88514e9eec1ae1ecf3c08e38501c49de34cf
@@ -0,0 +1,22 @@
require 'sinatra'
# add some data the first time or use an already existing db
configure :development do
load 'setup_activerecord.rb'
Person.create(:name => 'foo')
Person.create(:name => 'bar')
Person.create(:name => 'baz')
end
# sinatra-rest needs to be loaded after ActiveRecord
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../../lib')
require 'sinatra/rest'
rest Person
get '/' do
redirect '/people'
end
@@ -0,0 +1,12 @@
class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :people
end
end
@@ -0,0 +1,15 @@
require 'activerecord'
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger.level = Logger::WARN
config = {
:adapter => 'sqlite3',
:database => "db/#{Sinatra::Application.environment.to_s}.sqlite3"
}
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Migrator.up('db/migrate')
class Person < ActiveRecord::Base
end
@@ -0,0 +1,50 @@
!!! XML
!!! Strict
%html{html_attrs}
%head
%meta{:"http-equiv"=>"content-type", :content=>"text/html; charset=UTF-8"}/
%title People
%style{:type=>"text/css", :media=>"screen"}
:sass
body
font-family: "Lucida Grande", sans-serif
font-size: 12px
background-color: #E9ECEB
color: #555555
#container
width: 400px
margin: 25px auto
padding: 10px 25px
background-color: #FFFFFF
border: 1px solid #D4D4D4
:-moz-border-radius 8px
:-webkit-border-radius 8px
#footer
text-align: center
color: #909090
h1
color: #383838
font-size: 2em
font-weight: bold
a
color: #075FB2
text-decoration: none
.buttons *
padding: 0 0.5em
text-align: center
margin: 1em 0
display: inline
%body
#container
= yield
#footer
powered by
%a{:href=>'http://blindgaenger.github.com/sinatra-rest/'} sinatra-rest
@@ -0,0 +1,9 @@
%h1 Edit
%form{:action=>url_for_people_update(@person), :method=>"post"}
%input{:type=>"hidden", :name=>"_method", :value=>"put"}
Name:
%input{:name=>"name", :value=>@person.name}
.buttons
%a{:href => url_for_people_show(@person)} Cancel
%button{:type => :submit} Update
@@ -0,0 +1,11 @@
%h1 Index
%ul
- @people.each do |person|
%li
= person.id
%a{:href => url_for_people_show(person)}
= person.name
.buttons
%a{:href => url_for_people_new} New
@@ -0,0 +1,9 @@
%h1 New
%form{:action=>url_for_people_create, :method=>"post"}
Name:
%input{:name=>"name"}
.buttons
%a{:href => url_for_people_index} Cancel
%button{:type => :submit} Create
@@ -0,0 +1,20 @@
%h1 Show
%table
%tr
%th Name
%th Value
%tr
%td id
%td= @person.id
%tr
%td Name
%td= @person.name
.buttons
%a{:href => url_for_people_index} « Index
%a{:href => url_for_people_edit(@person)} Edit
%form{:action=>url_for_people_update(@person), :method=>"post"}
%input{:type=>"hidden", :name=>"_method", :value=>"delete"}
%button{:type => :submit} Delete

0 comments on commit 614f885

Please sign in to comment.