質問編集履歴

2

コードを追加しました

2020/12/14 00:20

投稿

takaaki919
takaaki919

スコア2

test CHANGED
File without changes
test CHANGED
@@ -54,7 +54,9 @@
54
54
 
55
55
 
56
56
 
57
+ ```
58
+
57
- ```posts_controller
59
+ posts_controller
58
60
 
59
61
  def index
60
62
 

1

コードを追加しました

2020/12/14 00:20

投稿

takaaki919
takaaki919

スコア2

test CHANGED
File without changes
test CHANGED
@@ -51,3 +51,135 @@
51
51
  <br />
52
52
 
53
53
  ```
54
+
55
+
56
+
57
+ ```posts_controller
58
+
59
+ def index
60
+
61
+ @posts = Post.all.order(created_at: :desc)
62
+
63
+ end
64
+
65
+
66
+
67
+ def show
68
+
69
+ @post = Post.find_by(id: params[:id])
70
+
71
+ @user = @post.user
72
+
73
+ @post = Post.find(params[:id])
74
+
75
+ @comments = @post.comments
76
+
77
+ @comment = Comment.new
78
+
79
+ end
80
+
81
+
82
+
83
+ def new
84
+
85
+ @post = Post.new
86
+
87
+ end
88
+
89
+
90
+
91
+ def create
92
+
93
+ @post = Post.new(
94
+
95
+ content: params[:content],
96
+
97
+ user_id: @current_user.id,
98
+
99
+ )
100
+
101
+ if params[:post].present?
102
+
103
+ @post.video = params[:post][:video]
104
+
105
+ print params
106
+
107
+ end
108
+
109
+ if @post.save
110
+
111
+ flash[:notice] = "投稿を作成しました"
112
+
113
+ redirect_to("/posts/index")
114
+
115
+ else
116
+
117
+ render("posts/new")
118
+
119
+ end
120
+
121
+ end
122
+
123
+
124
+
125
+ def edit
126
+
127
+ @post = Post.find_by(id: params[:id])
128
+
129
+ end
130
+
131
+
132
+
133
+ def update
134
+
135
+ @post = Post.find_by(id: params[:id])
136
+
137
+ @post.content = params[:content]
138
+
139
+ if @post.save
140
+
141
+ flash[:notice] = "投稿を編集しました"
142
+
143
+ redirect_to("/posts/index")
144
+
145
+ else
146
+
147
+ render("posts/edit")
148
+
149
+ end
150
+
151
+ end
152
+
153
+
154
+
155
+ def destroy
156
+
157
+ @post = Post.find_by(id: params[:id])
158
+
159
+ @post.destroy
160
+
161
+ flash[:notice] = "投稿を削除しました"
162
+
163
+ redirect_to("/posts/index")
164
+
165
+ end
166
+
167
+
168
+
169
+ def ensure_correct_user
170
+
171
+ @post = Post.find_by(id: params[:id])
172
+
173
+ if @post.user_id != @current_user.id
174
+
175
+ flash[:notice] = "権限がありません"
176
+
177
+ redirect_to("/posts/index")
178
+
179
+ end
180
+
181
+ end
182
+
183
+ end
184
+
185
+ ```