Skip to content

Commit a07b5ee

Browse files
committed
Admins can edit posts
1 parent ded3b37 commit a07b5ee

5 files changed

Lines changed: 52 additions & 1 deletion

File tree

features/blog.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ Feature: The Hackety Blog
2626
Given I'm logged in
2727
And I go to the new post page
2828
Then I should see "Sorry, buddy"
29+
Scenario: Admins may edit a post
30+
Given I'm logged in as admin
31+
And there is a blog post with the title "First Post"
32+
And I go to the edit post page for the post with the title "First Post"
33+
And I fill in "Title" with "new title"
34+
And I fill in "Body" with "new body"
35+
When I press "Modify Post"
36+
Then I should see "Post Modified"
37+
And I am on the post page for the post with the title "new title"
38+
And I should see "new title"
39+
And I should see "new body"
40+

features/support/paths.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ def path_to(page_name)
2121

2222
when /the posts page/
2323
'/posts'
24+
25+
when /the edit post page for the post with the title "(.*)"/
26+
post = Post.first(:title => $1)
27+
"/posts/#{post.id}/edit"
28+
29+
when /the post page for the post with the title "(.*)"/
30+
post = Post.first(:title => $1)
31+
"/posts/#{post.id}"
2432

2533
# Add more mappings here.
2634
# Here is an example that pulls values out of the Regexp:

hackety.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,15 @@
5656
@post = Post.find(params[:id])
5757
haml :posts_show
5858
end
59+
60+
get "/posts/:id/edit" do
61+
@post = Post.find(params[:id])
62+
haml :posts_edit
63+
end
64+
65+
put "/posts/:id" do
66+
@post = Post.find(params[:id])
67+
@post.update_attributes(params)
68+
flash[:notice] = "Post Modified"
69+
redirect "/posts/#{@post.id}"
70+
end

spec/hackety_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,17 @@ def app; Sinatra::Application; end
1313
describe "routes" do
1414
it "should display all posts at /posts" do
1515
post1 = mock("Post", :title => "title", :body => "body")
16-
post2 = mock("post", :title => "title", :body => "body")
16+
post2 = mock("Post", :title => "title", :body => "body")
1717
Post.should_receive(:all).and_return([post1,post2])
1818
get "/posts"
1919
last_response.status.should == 200
2020
end
21+
22+
it "should display the edit page at /posts/:id/edit" do
23+
post = mock("Post", :title => "title", :body => "body", :id => 42)
24+
Post.should_receive(:find).with("42").and_return(post)
25+
get "/posts/#{post.id}/edit"
26+
last_response.status.should == 200
27+
end
2128
end
2229
end

views/posts_edit.haml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
%form{:method => "POST", :action => "/posts/#{@post.id}" }
2+
%input{ :type => 'hidden', :name => "_method", :value => "PUT" }
3+
%label{:for => 'post_title'} Title
4+
%br
5+
%input{ :type => 'text', :name => 'title', :id => 'post_title', :value => @post.title}
6+
%br
7+
%label{:for => 'post_body'} Body
8+
%br
9+
%textarea{ :rows => '10', :cols => '30', :name => 'body', :id => 'post_body' }
10+
= @post.body
11+
%br
12+
%input{:type => 'submit', :value => "Modify Post"}

0 commit comments

Comments
 (0)