1
1
class CommentsController < ApplicationController
2
2
3
- before_action :access_required , only : [ :new , :edit , :update , :destroy ]
4
- before_action :verify_ownership , only : [ :edit , :update , :destroy ]
5
- before_action :require_admin! , only : [ :flag , :index ]
3
+ before_action :access_required , only : [ :update , :destroy ]
6
4
before_action :lookup_comment , only : [ :edit , :update , :destroy , :like ]
5
+ before_action :verify_ownership , only : [ :edit , :update , :destroy ]
7
6
before_action :lookup_protip , only : [ :create ]
8
7
9
- def index
10
- @comments = Comment . where ( 'created_at > ?' , 1 . day . ago )
11
- end
12
-
13
- def new ; end
14
-
15
- def edit ; end
16
-
17
8
def create
18
9
create_comment_params = params . require ( :comment ) . permit ( :comment )
19
10
@@ -25,10 +16,10 @@ def create
25
16
26
17
if @comment . save
27
18
record_event ( 'created comment' )
28
- format . html { redirect_to protip_path ( @comment . commentable ) }
19
+ format . html { redirect_to protip_path ( params [ :protip_id ] ) }
29
20
format . json { render json : @comment , status : :created , location : @comment }
30
21
else
31
- format . html { redirect_to protip_path ( @comment . commentable ) , error : "could not add your comment. try again" }
22
+ format . html { redirect_to protip_path ( params [ :protip_id ] ) , error : "could not add your comment. try again" }
32
23
format . json { render json : @comment . errors , status : :unprocessable_entity }
33
24
end
34
25
end
@@ -39,10 +30,10 @@ def update
39
30
40
31
respond_to do |format |
41
32
if @comment . update_attributes ( update_comment_params )
42
- format . html { redirect_to protip_path ( @comment . commentable . try ( :public_id ) ) }
33
+ format . html { redirect_to protip_path ( params [ :protip_id ] ) }
43
34
format . json { head :ok }
44
35
else
45
- format . html { redirect_to protip_path ( @comment . commentable . try ( :public_id ) ) , error : "could not update your comment. try again" }
36
+ format . html { redirect_to protip_path ( params [ :protip_id ] ) , error : "could not update your comment. try again" }
46
37
format . json { render json : @comment . errors , status : :unprocessable_entity }
47
38
end
48
39
end
@@ -70,18 +61,15 @@ def like
70
61
private
71
62
72
63
def lookup_comment
73
- id = params . permit ( :id ) [ :id ]
74
- @comment = Comment . find ( id )
64
+ @comment = Comment . find ( params [ :id ] )
75
65
lookup_protip
76
66
end
77
67
78
68
def lookup_protip
79
- protip_id = params . permit ( :protip_id ) [ :protip_id ]
80
- @protip = Protip . with_public_id ( protip_id )
69
+ @protip = Protip . find_by_public_id ( params [ :protip_id ] )
81
70
end
82
71
83
72
def verify_ownership
84
- lookup_comment
85
73
redirect_to ( root_url ) unless ( is_admin? or ( @comment && @comment . authored_by? ( current_user ) ) )
86
74
end
87
75
end
0 commit comments