File tree Expand file tree Collapse file tree 2 files changed +20
-11
lines changed Expand file tree Collapse file tree 2 files changed +20
-11
lines changed Original file line number Diff line number Diff line change 1
1
class ProjectsController < ApplicationController
2
+ before_action :set_project , only : [ :show ,
3
+ :edit ,
4
+ :update ,
5
+ :destroy ]
2
6
def index
3
7
@projects = Project . all
4
8
end
@@ -19,17 +23,7 @@ def create
19
23
end
20
24
end
21
25
22
- def show
23
- @project = Project . find ( params [ :id ] )
24
- end
25
-
26
- def edit
27
- @project = Project . find ( params [ :id ] )
28
- end
29
-
30
26
def update
31
- @project = Project . find ( params [ :id ] )
32
-
33
27
if @project . update ( project_params )
34
28
flash [ :notice ] = "Project has been updated."
35
29
redirect_to @project
@@ -40,7 +34,6 @@ def update
40
34
end
41
35
42
36
def destroy
43
- @project = Project . find ( params [ :id ] )
44
37
@project . destroy
45
38
46
39
flash [ :notice ] = "Project has been destroyed."
@@ -50,6 +43,15 @@ def destroy
50
43
51
44
private
52
45
46
+ def set_project
47
+ @project = Project . find ( params [ :id ] )
48
+ rescue ActiveRecord ::RecordNotFound
49
+ flash [ :alert ] = "The project you were looking" +
50
+ " for could not be found."
51
+
52
+ redirect_to projects_path
53
+ end
54
+
53
55
def project_params
54
56
params . require ( :project ) . permit ( :name , :description )
55
57
end
Original file line number Diff line number Diff line change 1
1
require 'rails_helper'
2
2
3
3
RSpec . describe ProjectsController , :type => :controller do
4
+ it "displays an error for a missing project" do
5
+ get :show , id : "not-here"
4
6
7
+ expect ( response ) . to redirect_to ( projects_path )
8
+ message = "The project you were looking for could not be found."
9
+
10
+ expect ( flash [ :alert ] ) . to eql ( message )
11
+ end
5
12
end
You can’t perform that action at this time.
0 commit comments