Created
August 2, 2012 21:14
-
-
Save davidrichards/3240691 to your computer and use it in GitHub Desktop.
A basic sinatra example
This file contains hidden or 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 'sinatra' | |
require 'json' | |
require_relative './database.rb' | |
get '/accounts' do | |
content_type :json | |
begin | |
@user = User.find(params[:user_id]) | |
{:accounts => @user.accounts.map(&:attributes)}.to_json | |
rescue ActiveRecord::RecordNotFound => e | |
[].to_json | |
end | |
end |
This file contains hidden or 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 'sinatra' | |
require 'sinatra/activerecord' | |
set :database, 'sqlite:///db/gateway.db' | |
class User < ActiveRecord::Base | |
has_many :accounts | |
def full_name | |
[first_name, last_name].compact.join(" ") | |
end | |
end | |
class Account < ActiveRecord::Base | |
belongs_to :user | |
end |
This file contains hidden or 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_relative './database' | |
require 'sinatra/activerecord/rake' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment