Skip to content

Instantly share code, notes, and snippets.

@jayfallon
Created January 6, 2010 02:02
Show Gist options
  • Save jayfallon/269934 to your computer and use it in GitHub Desktop.
Save jayfallon/269934 to your computer and use it in GitHub Desktop.
require "rubygems"
require "mongomapper"
require "sinatra"
MongoMapper.database = "dbname"
class Example
include MongoMapper::Document
key :name, String
timestamps!
has_many :join
has_many :another_class, :through => :join
end
get '/' do
erb :index
end
get '/new' do
erb :new
end
post '/create' do
@example = example.new(params[:example])
if @example.save
redirect "/show/#{@example.id}"
else
redirect('/list')
end
end
get '/show/:id' do
@example = example.find(params[:id])
if @example
erb :show
else
redirect('/list')
end
end
get '/list' do
@examples = example.all(:order => "name")
erb :list
end
get '/edit/:id' do
@example = example.find(params[:id])
erb :edit
end
post '/update/:id' do
@example = example.find(params[:id])
if @example.update_attributes(params[:example])
redirect "/show/#{@example.id}"
else
redirect('/list')
end
end
get '/delete/:id' do
example = example.find(params[:id])
example.destroy unless example.nil?
redirect('/list')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment