質問編集履歴

1

追記

2021/05/01 02:04

投稿

pay_561
pay_561

スコア26

test CHANGED
File without changes
test CHANGED
@@ -84,4 +84,178 @@
84
84
 
85
85
  ```
86
86
 
87
+
88
+
89
+ ##追記
90
+
91
+
92
+
93
+ ##posts_controller.rb
94
+
95
+ ```ここに言語を入力
96
+
97
+ class PostsController < ApplicationController
98
+
99
+ before_action :authenticate_user!
100
+
101
+ before_action :find_post, only: [:index, :show, :edit, :update, :destory]
102
+
103
+ before_action :force_redirect_unless_my_post, only: [:edit, :update, :destory]
104
+
105
+
106
+
107
+ def index
108
+
109
+ @posts = Post.all.order(created_at: :desc)
110
+
111
+ @posts = Post.page(params[:page]).per(25).order('updated_at DESC')
112
+
113
+ end
114
+
115
+
116
+
117
+ def show
118
+
119
+ @post = Post.find_by(id: params[:id])
120
+
121
+ end
122
+
123
+
124
+
125
+ def new
126
+
127
+ @post = Post.new
128
+
129
+ end
130
+
131
+
132
+
133
+ def edit
134
+
135
+ end
136
+
137
+
138
+
139
+ def create
140
+
141
+ @post = Post.new(post_params)
142
+
143
+ @post.user = current_user
144
+
145
+ if @post.save!
146
+
147
+ redirect_to root_path, notice: '投稿成功!!'
148
+
149
+ else
150
+
151
+ render :new
152
+
153
+ end
154
+
155
+ end
156
+
157
+
158
+
159
+ def update
160
+
161
+ if @post.update(post_params)
162
+
163
+ redirect_to root_path, notice: '投稿を更新しました。'
164
+
165
+ else
166
+
167
+ render :edit
168
+
169
+ end
170
+
171
+ end
172
+
173
+
174
+
175
+ def destory
176
+
177
+ if @post.destory
178
+
179
+ redirect_to root_path, notice: '投稿を削除しました。'
180
+
181
+ else
182
+
183
+ redirect_to root_path, alert: '投稿が削除できませんでした。'
184
+
185
+ end
186
+
187
+ end
188
+
189
+
190
+
191
+ private
192
+
193
+
194
+
195
+ def post_params
196
+
197
+ params.require(:post).permit(
198
+
199
+ :thumbnail,
200
+
201
+ :title,
202
+
203
+ :version,
204
+
205
+ :code,
206
+
207
+ :description,
208
+
209
+ :video,
210
+
211
+ images: [],
212
+
87
-
213
+ )
214
+
215
+ end
216
+
217
+
218
+
219
+ def find_post
220
+
221
+ @post = Post.find_by(params[:id])
222
+
223
+ end
224
+
225
+
226
+
227
+ def force_redirect_unless_my_post
228
+
229
+ return redirect_to root_path, alert: '自分の投稿ではありません' if @post.user != current_user
230
+
231
+ end
232
+
233
+ end
234
+
235
+
236
+
237
+ ```
238
+
239
+ ##profiles_controller.rb
240
+
241
+ ```ここに言語を入力
242
+
243
+ class ProfilesController < ApplicationController
244
+
245
+ before_action :authenticate_user!
246
+
247
+
248
+
249
+ def show
250
+
251
+ @profile = current_user
252
+
253
+ end
254
+
255
+
256
+
257
+ private
258
+
259
+ end
260
+
261
+ ```