-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
146 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
source 'https://rubygems.org' | ||
ruby '1.9.3' | ||
gem 'mongoid' | ||
gem 'sinatra' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
activemodel (3.2.14) | ||
activesupport (= 3.2.14) | ||
builder (~> 3.0.0) | ||
activesupport (3.2.14) | ||
i18n (~> 0.6, >= 0.6.4) | ||
multi_json (~> 1.0) | ||
builder (3.0.4) | ||
i18n (0.6.4) | ||
mongoid (3.1.4) | ||
activemodel (~> 3.2) | ||
moped (~> 1.4) | ||
origin (~> 1.0) | ||
tzinfo (~> 0.3.22) | ||
moped (1.5.0) | ||
multi_json (1.7.8) | ||
origin (1.1.0) | ||
rack (1.5.2) | ||
rack-protection (1.5.0) | ||
rack | ||
sinatra (1.4.3) | ||
rack (~> 1.4) | ||
rack-protection (~> 1.4) | ||
tilt (~> 1.3, >= 1.3.4) | ||
tilt (1.4.1) | ||
tzinfo (0.3.37) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
mongoid | ||
sinatra |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ class User | |
## Sinatra app | ||
## =========== | ||
|
||
# display form | ||
# display signup form | ||
get '/' do | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,16 +11,45 @@ class User | |
end | ||
|
||
|
||
# How many users are there currently in the database | ||
User.count | ||
### Create a new user | ||
|
||
u1 = User.new(:name => "Tom", :email => '[email protected]') | ||
|
||
# get the properties | ||
u1.name | ||
u1.email | ||
|
||
# change the name | ||
u1.name = "Tom Close" | ||
|
||
# check the name has changed | ||
u1.name | ||
|
||
# save the user to the database | ||
u1.save | ||
|
||
u = User.new(:name => "Tom", :email => '[email protected]') | ||
# TODO: create another two users | ||
|
||
u.save | ||
### Using the database | ||
# ==================== | ||
|
||
u1 = User.first | ||
# To find out about what's in the database, we use methods of User | ||
|
||
# How many users are there currently in the database | ||
User.count | ||
|
||
User.all {|u| puts u.name} | ||
# find the first user | ||
u2 = User.first | ||
|
||
# check the properties | ||
u2.name | ||
u2.email | ||
|
||
# find a specific user | ||
u3 = User.find_by(:name => "Tom Close") | ||
|
||
# pull all users out of the database | ||
users = User.all | ||
|
||
# print out their names | ||
users.each {|u| puts u.name} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'sinatra' | ||
require 'mongoid' | ||
require 'json' | ||
|
||
## Mongoid setup | ||
## ============= | ||
|
||
Mongoid.load!("mongoid.yml", :development) | ||
|
||
class Fruit | ||
include Mongoid::Document | ||
|
||
field :name | ||
end | ||
|
||
## Sinatra app | ||
## =========== | ||
|
||
get '/' do | ||
@fruits = Fruit.all | ||
erb :index | ||
end | ||
|
||
post '/' do | ||
name = params[:fruit_name] | ||
f = Fruit.new(:name => name) | ||
f.save | ||
@fruits = Fruit.all | ||
erb :index | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
development: | ||
sessions: | ||
default: | ||
hosts: | ||
- localhost:27017 | ||
database: mongo1_example | ||
options: | ||
raise_not_found_error: false |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<h1>List of Fruits</h1> | ||
|
||
|
||
<ul> | ||
<% @fruits.each do |f| %> | ||
<li><%= f.name %></li> | ||
<% end %> | ||
</ul> | ||
|
||
<h3>Add a new fruit</h3> | ||
|
||
<form method='post'> | ||
<label>Name of fruit</label> | ||
<input type='text' name='fruit_name'> | ||
<input type='submit'> | ||
</form> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Fruit</title> | ||
<!-- Latest compiled and minified CSS --> | ||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/css/bootstrap.min.css"> | ||
|
||
<link rel="stylesheet" href="/dist/css/bootstrap.min.css"> | ||
</head> | ||
<body> | ||
<div class='container'> | ||
|
||
<%= yield %> | ||
|
||
</div> | ||
</body> | ||
</html> |
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
Empty file.
Empty file.