Created
April 13, 2012 22:02
-
-
Save anonymous/2380428 to your computer and use it in GitHub Desktop.
Sinatra + Mongo
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/base' | |
require './models/contact' | |
require './models/phone' | |
require 'mongoid' | |
class Api < Sinatra::Base | |
before do | |
content_type :json | |
end | |
get '/contacts' do | |
Contact.all.to_json | |
end | |
post '/contacts' do | |
@contact = Contact.create(params[:contact]) | |
@contact.to_json | |
end | |
end |
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 "mongoid" | |
class Contact | |
include Mongoid::Document | |
field :name, type: String | |
field :email, type: String | |
field :twitter, type: String | |
field :github, type: String | |
has_many :phones, :autosave => true | |
accepts_nested_attributes_for :phones | |
end |
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 'mongoid' | |
class Phone | |
include Mongoid::Document | |
field :number, type: String | |
field :type, type: String | |
attr_accessible :number, :type | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment