Skip to content
This repository has been archived by the owner on Jan 8, 2019. It is now read-only.

Commit

Permalink
Show 404 when MongoMapper::DocumentNotFound is raised
Browse files Browse the repository at this point in the history
  • Loading branch information
drewreeve committed Mar 28, 2012
1 parent 595b0bd commit 73769af
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
7 changes: 6 additions & 1 deletion apps/_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ def find_template(views, name, engine, &block)
@page_title = "404 Not Found"
erb :'404', :layout => false, :views => settings.root + '/views'
end

error MongoMapper::DocumentNotFound do
@page_title = "404 Not Found"
erb :'404', :layout => false, :views => settings.root + '/views/'
end

end

end
end
4 changes: 2 additions & 2 deletions apps/blog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class BlogApp < Base
end

get '/post/:slug' do
@post = Post.find_by_slug(params[:slug])
@post = Post.find_by_slug!(params[:slug])
@page_title = @post.title
erb :detail
end

end
end
end
8 changes: 4 additions & 4 deletions apps/manage_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class ManagePostsApp < AdminApp
end

get '/new/:type' do
raise not_found unless valid_post_type?(params[:type])
not_found unless valid_post_type?(params[:type])
@post = build_post(params[:type], params[:post])
@page_title = "New Post"
erb :new
end

post '/' do
raise not_found unless valid_post_type?(params[:type])
not_found unless valid_post_type?(params[:type])
@post = build_post(params[:type], params[:post])

if @post.save
Expand All @@ -33,7 +33,7 @@ class ManagePostsApp < AdminApp
end

get '/:slug/edit' do
@post = Post.where(:slug => params[:slug]).first
@post = Post.find_by_slug!(params[:slug])
@page_title = "Editing #{@post.title}"
erb :edit
end
Expand Down Expand Up @@ -72,4 +72,4 @@ class ManagePostsApp < AdminApp
end

end
end
end
7 changes: 6 additions & 1 deletion spec/apps/blog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@
visit "/post/#{a.slug}"
page.should have_content(a.title)
end

it "should show 404 if post not found" do
visit "/post/non-existent-post"
page.should have_content(404)
end

end
end

0 comments on commit 73769af

Please sign in to comment.