-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rb
More file actions
50 lines (38 loc) · 1.17 KB
/
server.rb
File metadata and controls
50 lines (38 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Allison Browne
# Recipe-Box Systems Check
# 11/28/14
# First Commit
require 'pg'
require 'sinatra'
require 'pry'
require 'sinatra/reloader'
def db_connection
begin
connection = PG.connect(dbname: 'recipes')
yield(connection)
# allows this method to accept a block
# of code (in the form of a do..end or {..} block) that can be run in the middle of the method.
ensure
connection.close
end
end
get '/recipes' do
db_connection do |conn|
@recipes = conn.exec_params('SELECT recipes.id, recipes.name FROM recipes ORDER BY recipes.name')
end
erb :'index'
end
get '/recipes/:id' do
@recipe_id = params[:id]
#* The page must include the recipe name, description, and instructions.
#* The page must list the ingredients required for the recipe.
db_connection do |conn|
@recipe_info = conn.exec_params("SELECT id, name, description, instructions FROM recipes
WHERE recipes.id = #{@recipe_id}")
end
db_connection do |connection|
@recipe_ingredients = connection.exec_params("SELECT ingredients.name FROM ingredients
WHERE ingredients.recipe_id = #{@recipe_id}")
end
erb :'show'
end