Created
October 10, 2013 00:46
-
-
Save spencereldred/6911202 to your computer and use it in GitHub Desktop.
guestbook app with database. uses the params[:name] and params[:message] to create sql query.
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 'sinatra' | |
require 'sinatra/reloader' | |
require 'sqlite3' | |
db = SQLite3::Database.new('guestbook.sqlite3'); | |
sql = "select * from guestbook;" | |
sql_name = "select name from guestbook where id = 1" | |
@result = db.execute(sql); | |
p @result | |
# messages = [ {"Bob"=> "hello"}, {"Harriet" => "how are you"}, {"Josh" => "cool"} ] | |
messages = []; | |
def init | |
@title = "Guestbook" | |
@heading = "Welcome to the Guestbook App" | |
end | |
get '/write/:name/:message' do | |
init | |
@subtitle = "Write a message in the URL tab: /name/message." | |
messages << { params[:name] => params[:message] } | |
# create the sql query from the data passed in the params | |
# could have used instance variables and string interpolation here: @name = params[:name] and "#{@name}" | |
sql_insert = "insert into guestbook(user_name, post) values ('" + params[:name] +"', '" + params[:message] + "' );" | |
p sql_insert | |
db.execute(sql_insert); | |
@result = db.execute(sql); | |
p @result | |
@flag = 2 | |
erb :guestbook_message | |
end | |
get '/message' do | |
init | |
@caption = "Guestbook" | |
# get the data from the database | |
sql = "select * from guestbook;" | |
sql_name = "select name from guestbook where id = 1" | |
@result = db.execute(sql); | |
p @result | |
# Load the messages array with hashes made from the data retrieved from the db | |
i = 0 | |
messages = [] | |
while i < @result.length | |
messages << { @result[i][1].to_s => @result[i][2].to_s } | |
i += 1 | |
end | |
@messages = messages | |
@flag = 1 | |
erb :guestbook_message | |
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
<!DOCTYPE html> | |
<head lang="en"> | |
<meta char="UTF-8"> | |
<link href='http://fonts.googleapis.com/css?family=Kite+One' rel='stylesheet' type='text/css'> | |
<style type="text/css"> | |
body { | |
background: lightblue; | |
text-align: center; | |
font-family: 'Kite One', sans-serif; | |
} | |
table { | |
text-align: left; | |
margin: auto; | |
} | |
caption { | |
font-size: 2em; | |
text-align: center; | |
} | |
th { | |
font-size: 1.2em; | |
} | |
.name { | |
width: 80px; | |
} | |
.message { | |
width: 240px; | |
} | |
</style> | |
<title><%= @title %></title> | |
</head> | |
<body> | |
<% if @flag == 1 %> | |
<header> | |
<h1><%= @heading %></h1> | |
</header> | |
<section> | |
<article> | |
<h2><%= @subtitle %></h2> | |
<table border="2"> | |
<caption><%= @caption %></caption> | |
<tr> | |
<th>Name </th> | |
<th>Message</th> | |
</tr> | |
<% @messages.each do |message| %> | |
<% message.each do |k,v| %> | |
<tr> | |
<td class="name"> <%= k %></td> | |
<td class="message"><%= v %></td> | |
</tr> | |
<% end %> | |
<% end %> | |
</table> | |
</article> | |
</section> | |
<% else %> | |
<header> | |
<h1><%= @heading %></h1> | |
</header> | |
<section> | |
<article> | |
<h2><%= @subtitle %></h2> | |
</article> | |
</section> | |
<% end %> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment