質問編集履歴
2
コードを追加しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -26,7 +26,8 @@
|
|
26
26
|
<br />
|
27
27
|
```
|
28
28
|
|
29
|
+
```
|
29
|
-
|
30
|
+
posts_controller
|
30
31
|
def index
|
31
32
|
@posts = Post.all.order(created_at: :desc)
|
32
33
|
end
|
1
コードを追加しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -24,4 +24,70 @@
|
|
24
24
|
<% end %>
|
25
25
|
</div>
|
26
26
|
<br />
|
27
|
+
```
|
28
|
+
|
29
|
+
```posts_controller
|
30
|
+
def index
|
31
|
+
@posts = Post.all.order(created_at: :desc)
|
32
|
+
end
|
33
|
+
|
34
|
+
def show
|
35
|
+
@post = Post.find_by(id: params[:id])
|
36
|
+
@user = @post.user
|
37
|
+
@post = Post.find(params[:id])
|
38
|
+
@comments = @post.comments
|
39
|
+
@comment = Comment.new
|
40
|
+
end
|
41
|
+
|
42
|
+
def new
|
43
|
+
@post = Post.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def create
|
47
|
+
@post = Post.new(
|
48
|
+
content: params[:content],
|
49
|
+
user_id: @current_user.id,
|
50
|
+
)
|
51
|
+
if params[:post].present?
|
52
|
+
@post.video = params[:post][:video]
|
53
|
+
print params
|
54
|
+
end
|
55
|
+
if @post.save
|
56
|
+
flash[:notice] = "投稿を作成しました"
|
57
|
+
redirect_to("/posts/index")
|
58
|
+
else
|
59
|
+
render("posts/new")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def edit
|
64
|
+
@post = Post.find_by(id: params[:id])
|
65
|
+
end
|
66
|
+
|
67
|
+
def update
|
68
|
+
@post = Post.find_by(id: params[:id])
|
69
|
+
@post.content = params[:content]
|
70
|
+
if @post.save
|
71
|
+
flash[:notice] = "投稿を編集しました"
|
72
|
+
redirect_to("/posts/index")
|
73
|
+
else
|
74
|
+
render("posts/edit")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def destroy
|
79
|
+
@post = Post.find_by(id: params[:id])
|
80
|
+
@post.destroy
|
81
|
+
flash[:notice] = "投稿を削除しました"
|
82
|
+
redirect_to("/posts/index")
|
83
|
+
end
|
84
|
+
|
85
|
+
def ensure_correct_user
|
86
|
+
@post = Post.find_by(id: params[:id])
|
87
|
+
if @post.user_id != @current_user.id
|
88
|
+
flash[:notice] = "権限がありません"
|
89
|
+
redirect_to("/posts/index")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
27
93
|
```
|