Skip to content

Commit e290ba4

Browse files
committed
Switch to thin and fix some ajax bugs
1 parent b73ff8b commit e290ba4

File tree

5 files changed

+61
-27
lines changed

5 files changed

+61
-27
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ gem 'sqlite3'
55
gem 'rake'
66

77
gem 'sinatra_more'
8+
gem 'thin'

Gemfile.lock

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ GEM
1616
tzinfo (~> 1.1)
1717
arel (6.0.3)
1818
builder (3.2.2)
19+
daemons (1.2.3)
20+
eventmachine (1.0.9.1)
1921
i18n (0.7.0)
2022
json (1.8.3)
2123
minitest (5.8.3)
@@ -37,6 +39,10 @@ GEM
3739
thor (>= 0.11.8)
3840
tilt (>= 0.2)
3941
sqlite3 (1.3.11)
42+
thin (1.6.4)
43+
daemons (~> 1.0, >= 1.0.9)
44+
eventmachine (~> 1.0, >= 1.0.4)
45+
rack (~> 1.0)
4046
thor (0.19.1)
4147
thread_safe (0.3.5)
4248
tilt (2.0.2)
@@ -51,6 +57,7 @@ DEPENDENCIES
5157
sinatra-activerecord
5258
sinatra_more
5359
sqlite3
60+
thin
5461

5562
BUNDLED WITH
5663
1.10.6

app.rb

+18-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'active_record'
33
require 'sinatra'
44
require 'sinatra/activerecord'
5+
# require 'sinatra/json'
56

67
set :database, { adapter: 'sqlite3', database: 'treeyak.sqlite3' }
78

@@ -46,20 +47,25 @@ class Yak < ActiveRecord::Base
4647
redirect to('/')
4748
end
4849

50+
get '/wtf' do
51+
'hi'
52+
end
53+
4954
post '/upvote' do
55+
yak_id = params['yak_id']
56+
5057
# BEGIN YOUR CODE HERE
5158

52-
# In this section, you should increement the yak's upvotes by 1 and return the new number of upvotes in json.
59+
# In this section, you should increment the yak's upvotes by 1 and return the new number of upvotes in json.
5360
# (Note: This will be called via AJAX, so you don't need to render any html or redirect to any other page.)
5461

5562
# END YOUR CODE HERE
5663

5764
# SOLUTION
58-
yak = Yak.where(id: params['yak_id']).first
65+
yak = Yak.where(id: yak_id).first
5966
yak.upvotes += 1
60-
puts yak.upvotes
6167
yak.save
62-
yak.upvotes # return new number of upvotes to client
68+
yak.upvotes.to_s
6369
# END SOLUTION
6470
end
6571

@@ -68,21 +74,16 @@ class Yak < ActiveRecord::Base
6874

6975
# BEGIN YOUR CODE HERE
7076

71-
# In this section, you should decrement the yak's upvotes by 1. If the yak has less than -2 votes, then you should delete the yak and return JSON indicating that the yak has been deleted. Otherwise, return the new number of upvotes this yak has.
72-
# This will be called via AJAX, so you don't need to render any html or redirect to any other page.
73-
# Hint: You should be able to reuse some code from the '/upvote' method.
77+
# In this section, you should decrement the yak's upvotes by 1 and return the new number of upvotes in json.
78+
# (Note: This will be called via AJAX, so you don't need to render any html or redirect to any other page.)
7479

7580
# END YOUR CODE HERE
7681

7782
# SOLUTION
78-
yak = Yak.where(id: params['yak_id']).first
83+
yak = Yak.where(id: yak_id).first
7984
yak.upvotes -= 1
80-
if yak.upvotes <= -2
81-
yak.destroy!
82-
# render json to indicate yak has been flagged/deleted
83-
end
8485
yak.save
85-
yak.upvotes # return new number of upvotes to client
86+
yak.upvotes.to_s
8687
# END SOLUTION
8788
end
8889

@@ -97,6 +98,8 @@ class Yak < ActiveRecord::Base
9798
# SOLUTION
9899
yaks = Yak.order(upvotes: :desc)
99100
# END SOLUTION
101+
102+
erb :index, locals: { yaks: yaks }
100103
end
101104

102105
get '/new' do
@@ -110,4 +113,6 @@ class Yak < ActiveRecord::Base
110113
# SOLUTION
111114
yaks = Yak.order(created_at: :desc)
112115
# END SOLUTION
116+
117+
erb :index, locals: { yaks: yaks }
113118
end

treeyak.sqlite3

0 Bytes
Binary file not shown.

views/index.erb

+35-14
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<div id="subwelcome">
5656
<p><a href="/new_yak">Create a new yak</a></p>
57-
Sort by: <a href="/?order=new">New</a> | <a href="/?order=hot">Hot</a>
57+
Sort by: <a href="/new">New</a> | <a href="/hot">Hot</a>
5858
</div>
5959

6060

@@ -66,8 +66,13 @@
6666
<p><%= yak.contents %></p>
6767
</div>
6868
<div class="card-action">
69-
<a href="#">Upvote</a>
70-
<a href="#">Downvote</a>
69+
<span style="color: yellow;">
70+
<span id="<%= yak.id %>-upvotes"><%= yak.upvotes %></span> upvotes
71+
</span>
72+
<span style="float: right;">
73+
<a href="#" class="upvote" data-yakid="<%= yak.id %>">Upvote</a>
74+
<a href="#" class="downvote" data-yakid="<%= yak.id %>">Downvote</a>
75+
</span>
7176
</div>
7277
</div>
7378
</div>
@@ -77,21 +82,37 @@
7782

7883
<script type="text/javascript">
7984
$(document).ready(function() {
85+
8086
$('.upvote').on('click', function(e) {
81-
console.log("got upvote");
82-
console.log($(this).get('yakid'));
87+
e.preventDefault();
8388

84-
// $.ajax({
85-
// method: "POST",
86-
// url: "/upvote?yak_id=<%= yak.id %>"
87-
// })
88-
// .done(function(msg) {
89-
// console.log(msg);
90-
// });
89+
var yak_id = $(this).data('yakid');
9190

92-
// /upvote?yak_id=<%= yak.id %>
93-
// /downvote?yak_id=<%= yak.id %>
91+
$.ajax({
92+
method: "POST",
93+
url: "/upvote?yak_id=" + yak_id
94+
})
95+
.done(function(msg) {
96+
// console.log(msg);
97+
$("#" + yak_id + "-upvotes").text(msg);
98+
});
9499
});
100+
101+
$('.downvote').on('click', function(e) {
102+
e.preventDefault();
103+
104+
var yak_id = $(this).data('yakid');
105+
106+
$.ajax({
107+
method: "POST",
108+
url: "/downvote?yak_id=" + yak_id
109+
})
110+
.done(function(msg) {
111+
// console.log(msg);
112+
$("#" + yak_id + "-upvotes").text(msg);
113+
});
114+
});
115+
95116
});
96117
</script>
97118
</body>

0 commit comments

Comments
 (0)