Permalink
Browse files

Users should be redirected back to the projects page

                if they try going to a project that doesn't exist.
  • Loading branch information...
1 parent 9e13d54 commit c28f53ea475156b698473430f3779ef11f36aec2 @wycats committed Apr 28, 2011
Showing with 22 additions and 5 deletions.
  1. +11 −5 app/controllers/projects_controller.rb
  2. +11 −0 spec/controllers/projects_controller_spec.rb
@@ -1,4 +1,6 @@
class ProjectsController < ApplicationController
+ before_filter :find_project, :only => [:show, :edit, :update, :destroy]
+
def index
@projects = Project.scoped
end
@@ -18,12 +20,9 @@ def create
end
def edit
- @project = Project.find(params[:id])
end
def update
- @project = Project.find(params[:id])
-
if @project.update_attributes(params[:project])
redirect_to @project, :notice => "Project has been updated."
else
@@ -33,12 +32,19 @@ def update
end
def show
- @project = Project.find(params[:id])
end
def destroy
- @project = Project.find(params[:id])
@project.destroy
redirect_to projects_path, :notice => "Project has been deleted."
end
+
+private
+ def find_project
+ @project = Project.find(params[:id])
+ rescue ActiveRecord::RecordNotFound
+ flash[:alert] = "The project you were looking" +
+ " for could not be found."
+ redirect_to projects_path
+ end
end
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe ProjectsController do
+ it "displays an error for a missing project" do
+ get :show, :id => "not-here"
+ response.should redirect_to(projects_path)
+ message = "The project you were looking for could not be found."
+ flash[:alert].should eql(message)
+ end
+end
+

0 comments on commit c28f53e

Please sign in to comment.