Skip to content

Commit df8944d

Browse files
committed
requiring login to post content
1 parent a2de7a5 commit df8944d

3 files changed

Lines changed: 60 additions & 70 deletions

File tree

controllers/content_controller.rb

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,10 @@
99

1010
# We also need a simple POST that will create content.
1111
post "/content" do
12+
require_login!
1213

13-
# One small wrinkle: We're allowing anonymous posts. So, if we're not logged
14-
# in, we need to set the author and email properly so that all of our views
15-
# work.
16-
if current_user
17-
params[:content][:author] = current_user.username
18-
params[:content][:author_email] = current_user.email
19-
else
20-
params[:content][:author] = "anonymous"
21-
params[:content][:author_email] = "anonymous@example.com"
22-
end
14+
params[:content][:author] = current_user.username
15+
params[:content][:author_email] = current_user.email
2316
@content = Content.create(params[:content])
2417
flash[:notice] = "Thanks for your post!"
2518
redirect "/stream"
@@ -28,17 +21,11 @@
2821
# We're allowing comments to be made on posts, so we need a route for that as
2922
# well.
3023
post "/content/:id/comment" do
24+
require_login!
3125
@content = Content.first(:id => params[:id])
3226

33-
# We have the same wrinkle as we do when making posts: We're allowing
34-
# anonymous comments. So we have to properly set all that up.
35-
if current_user
36-
params[:comment][:author] = current_user.username
37-
params[:comment][:author_email] = current_user.email
38-
else
39-
params[:comment][:author] = "Anonymous"
40-
params[:comment][:author_email] = "anonymous@example.com"
41-
end
27+
params[:comment][:author] = current_user.username
28+
params[:comment][:author_email] = current_user.email
4229
@content.comments << Comment.new(params[:comment])
4330
@content.save
4431

views/content/show.haml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
:markdown
1616
#{reply.body}
1717
%div{:style => "clear:both"}
18-
%h2 Add a comment
19-
%form{:action => "/content/#{@content.id}/comment", :method => "POST"}
20-
%label{:for => "comment_body"} Body
21-
%br
22-
%textarea{:id => "comment_body", :name => "comment[body]", :rows => 20, :cols => 50}
23-
%br/
24-
Comments are formatted via
25-
%a{:href => "http://daringfireball.net/projects/markdown/syntax"} Markdown
26-
%br
27-
%input{:type => "submit", :value => "Submit", :class => "button"}
18+
- if logged_in?
19+
%h2 Add a comment
20+
%form{:action => "/content/#{@content.id}/comment", :method => "POST"}
21+
%label{:for => "comment_body"} Body
22+
%br
23+
%textarea{:id => "comment_body", :name => "comment[body]", :rows => 20, :cols => 50}
24+
%br/
25+
Comments are formatted via
26+
%a{:href => "http://daringfireball.net/projects/markdown/syntax"} Markdown
27+
%br
28+
%input{:type => "submit", :value => "Submit", :class => "button"}
29+
- else
30+
%a{:href => "/login"} Please log in to comment.
2831
%br/
2932
%br/
30-
33+

views/stream.haml

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,46 @@
22
#infobox{:style => "border:black 1px solid;padding:10px;margin:10px;"}
33
Hello there! This is the website for Hackety Hack. You can ask a question, share programs that you've made, or just tell everyone something. Don't forget, you should <a href="http://hackety-hack.com/download">download Hackety Hack</a>, it will teach you everything you need to get started with programming! This is just a place to participate in the Hackety Hack community.
44
.span-18.last
5-
#infobox{:style => "border:red 1px solid;padding:10px;margin:10px;"}
6-
Check it out! The newest feature: You can visit a list of the most recent updated programs, and check out some really cool featured ones. I've added a <a href="/programs">programs page</a> to show them off!
7-
.span-18.last
8-
%ul#input_list
9-
%li{:style => "display:inline"}
10-
%a#question_button.button{:href => "#"} Ask a question
11-
%li{:style => "display:inline"}
12-
%a#link_button.button{:href => "#"} Share a link
13-
%li{:style => "display:inline"}
14-
%a#post_button.button{:href => "#"} Tell everyone something
15-
%form#link_form{:action => "/content", :style => "display:none", :method => "POST"}
16-
%label{:for => "link"} Share a link
17-
%a.cancel{:href => "#"} x
18-
%br/
19-
%input#link{ :name => "content[body]", :size => "76", :type => "textbox"}/
20-
%input{ :name => "content[type]", :type => "hidden", :value => "link"}/
21-
%br/
22-
%input.button{:type => "submit"}
23-
%form#post_form{:action => "/content", :style => "display:none", :method => "POST"}
24-
%label{:for => "post"} Tell everyone something
25-
%a.cancel{:href => "#"} x
26-
%br/
27-
%textarea#post{ :name => "content[body]" }
28-
%input{ :name => "content[type]", :type => "hidden", :value => "post"}/
29-
%br/
30-
Comments are formatted via
31-
%a{:href => "http://daringfireball.net/projects/markdown/syntax"} Markdown
32-
%br/
33-
%input.button{:type => "submit"}
34-
%form#question_form{:action => "/content", :style => "display:none", :method => "POST"}
35-
%label{:for => "question"} Ask a question
36-
%a.cancel{:href => "#"} x
37-
%br/
38-
%textarea#question{:name => "content[body]"}
39-
%input{ :name => "content[type]", :type => "hidden", :value => "question"}/
40-
%br/
41-
Comments are formatted via
42-
%a{:href => "http://daringfireball.net/projects/markdown/syntax"} Markdown
43-
%br/
44-
%input.button{:type => "submit"}
5+
- if logged_in?
6+
%ul#input_list
7+
%li{:style => "display:inline"}
8+
%a#question_button.button{:href => "#"} Ask a question
9+
%li{:style => "display:inline"}
10+
%a#link_button.button{:href => "#"} Share a link
11+
%li{:style => "display:inline"}
12+
%a#post_button.button{:href => "#"} Tell everyone something
13+
%form#link_form{:action => "/content", :style => "display:none", :method => "POST"}
14+
%label{:for => "link"} Share a link
15+
%a.cancel{:href => "#"} x
16+
%br/
17+
%input#link{ :name => "content[body]", :size => "76", :type => "textbox"}/
18+
%input{ :name => "content[type]", :type => "hidden", :value => "link"}/
19+
%br/
20+
%input.button{:type => "submit"}
21+
%form#post_form{:action => "/content", :style => "display:none", :method => "POST"}
22+
%label{:for => "post"} Tell everyone something
23+
%a.cancel{:href => "#"} x
24+
%br/
25+
%textarea#post{ :name => "content[body]" }
26+
%input{ :name => "content[type]", :type => "hidden", :value => "post"}/
27+
%br/
28+
Comments are formatted via
29+
%a{:href => "http://daringfireball.net/projects/markdown/syntax"} Markdown
30+
%br/
31+
%input.button{:type => "submit"}
32+
%form#question_form{:action => "/content", :style => "display:none", :method => "POST"}
33+
%label{:for => "question"} Ask a question
34+
%a.cancel{:href => "#"} x
35+
%br/
36+
%textarea#question{:name => "content[body]"}
37+
%input{ :name => "content[type]", :type => "hidden", :value => "question"}/
38+
%br/
39+
Comments are formatted via
40+
%a{:href => "http://daringfireball.net/projects/markdown/syntax"} Markdown
41+
%br/
42+
%input.button{:type => "submit"}
43+
- else
44+
%a{:href => "/login"} Please log in to post something.
4545
.span-18
4646
%h2 Here's what everyone else is up to!
4747
%ul{:style => "list-style:none"}

0 commit comments

Comments
 (0)