Skip to content

Commit 2e3400a

Browse files
committed
Adding comments to programs.
1 parent 7064dc8 commit 2e3400a

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

controllers/programs.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
redirect "/programs/#{program.creator_username}/#{program.slug}"
1212
end
1313

14+
post "/programs/:username/:slug/comment" do
15+
@program = Program.first(:creator_username => params[:username], :slug => params[:slug])
16+
if current_user
17+
params[:comment][:author] = current_user.username
18+
params[:comment][:author_email] = current_user.email
19+
else
20+
params[:comment][:author] = "Anonymous"
21+
params[:comment][:author_email] = "anonymous@example.com"
22+
end
23+
@program.comments << Comment.new(params[:comment])
24+
@program.save
25+
26+
flash[:notice] = "Replied!"
27+
redirect "/programs/#{params[:username]}/#{params[:slug]}"
28+
end
29+
1430
get "/programs/:username.json" do
1531
programs = Program.all(:creator_username => params[:username])
1632
programs.to_json

models/program.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Program
88
validate_on_create :slug_check
99
before_save :make_slug
1010

11+
many :comments
12+
1113
private
1214
def slug_check
1315
programs = Program.all(:creator_username => creator_username)

views/programs/show.haml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,29 @@
2020

2121
%pre= @program.code
2222

23+
%hr/
24+
25+
%h2 Comments
26+
27+
- @program.comments.each do |reply|
28+
.comment{:style => "clear:both;padding-bottom:1em"}
29+
%div{:style => "float:left;padding-right:1em"}
30+
%a{:href => "/hackers/#{reply.author}"}
31+
%img{:src => gravatar_url_for(reply.author_email), :alt => "#{reply.author.downcase}_gravatar", :class => "comment_gravatar"}
32+
%div{:style => "width:500px"}
33+
%p
34+
<a href="/hackers/#{reply.author}">#{reply.author}</a> says:
35+
%p
36+
:markdown
37+
#{reply.body}
38+
%br/
39+
%br/
40+
%h2 Add a comment
41+
%form{:action => "/programs/#{@program.creator_username}/#{@program.slug}/comment", :method => "POST"}
42+
%label{:for => "comment_body"} Body
43+
%br
44+
%textarea{:id => "comment_body", :name => "comment[body]", :rows => 20, :cols => 50}
45+
%br
46+
%input{:type => "submit", :value => "Submit", :class => "button"}
47+
%br/
48+
%br/

0 commit comments

Comments
 (0)