-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
68 lines (48 loc) · 1.42 KB
/
app.rb
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
51
52
53
54
55
56
57
58
59
require 'sinatra'
require 'shotgun'
require 'pry'
require 'csv'
def read_beers_from(filename)
beers = []
CSV.foreach(filename, headers: true) do |row|
beers << {
name: row['name'],
description: row['description']
}
end
beers
end
def save_beer(filename, name, description)
CSV.open(filename, 'a') do |csv|
csv << [name, description]
end
end
get '/beers' do
@beers = read_beers_from('beers.csv')
erb :'beers/index'
end
get '/beers/new' do
erb :'beers/new'
end
post '/beers' do
# params looks like this:
# {"beer"=>{"name"=>"Pumpkinhead", "description"=>"Delicious!"}}
# take the beer params from params hash
@name = params[:beer][:name]
@description = params[:beer][:description]
if !params[:beer][:name].empty? && !params[:beer][:description].empty? && params[:beer][:description].length >= 4
# add the beer to the CSV
save_beer('beers.csv', @name, @description)
# redirect the user to the index page
redirect '/beers'
else
@error_messages = []
@error_messages << "You must enter a name." if params[:beer][:name].empty?
@error_messages << "You must enter a description." if params[:beer][:description].empty?
@error_messages << "Description must be 4 characters or longer." if params[:beer][:description].length < 4
# if params[:beer][:name].empty?
# @error_messages << "You must enter a name."
# end
erb :'beers/new'
end
end