質問編集履歴

9

変更

2020/01/16 09:53

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -294,6 +294,8 @@
294
294
 
295
295
  @post_images = PostImage.page(params[:page]).reverse_order
296
296
 
297
+ @post_image = PostImage.new
298
+
297
299
  @user = @post_image.user
298
300
 
299
301
  end

8

変更

2020/01/16 09:53

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -294,7 +294,7 @@
294
294
 
295
295
  @post_images = PostImage.page(params[:page]).reverse_order
296
296
 
297
- @user = PostImage.user
297
+ @user = @post_image.user
298
298
 
299
299
  end
300
300
 

7

問題時のコードに変更

2020/01/16 09:34

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -202,7 +202,7 @@
202
202
 
203
203
  def create
204
204
 
205
- @user = User.find(params[:relationship][:follow_id])
205
+ user = User.find(params[:relationship][:follow_id])
206
206
 
207
207
  following = current_user.follow(user)
208
208
 
@@ -210,13 +210,13 @@
210
210
 
211
211
  flash[:success] = 'ユーザーをフォローしました'
212
212
 
213
- redirect_to @user
213
+ redirect_to user
214
214
 
215
215
  else
216
216
 
217
217
  flash.now[:alert] = 'ユーザーのフォローに失敗しました'
218
218
 
219
- redirect_to @user
219
+ redirect_to user
220
220
 
221
221
  end
222
222
 
@@ -226,7 +226,7 @@
226
226
 
227
227
  def destroy
228
228
 
229
- @user = User.find(params[:relationship][:follow_id])
229
+ user = User.find(params[:relationship][:follow_id])
230
230
 
231
231
  following = current_user.unfollow(user)
232
232
 
@@ -234,13 +234,13 @@
234
234
 
235
235
  flash[:success] = 'ユーザーのフォローを解除しました'
236
236
 
237
- redirect_to @user
237
+ redirect_to user
238
238
 
239
239
  else
240
240
 
241
241
  flash.now[:alert] = 'ユーザーのフォロー解除に失敗しました'
242
242
 
243
- redirect_to @user
243
+ redirect_to user
244
244
 
245
245
  end
246
246
 
@@ -254,7 +254,7 @@
254
254
 
255
255
  def set_user
256
256
 
257
- @user = User.find(params[:relationship][:follow_id])
257
+ user = User.find(params[:relationship][:follow_id])
258
258
 
259
259
  end
260
260
 
@@ -294,7 +294,7 @@
294
294
 
295
295
  @post_images = PostImage.page(params[:page]).reverse_order
296
296
 
297
- @user = @post_image.id
297
+ @user = PostImage.user
298
298
 
299
299
  end
300
300
 
@@ -364,7 +364,7 @@
364
364
 
365
365
  def update
366
366
 
367
- @user = User.finr(params[:id])
367
+ @user = User.find(params[:id])
368
368
 
369
369
  @user.update(user_params)
370
370
 
@@ -386,6 +386,8 @@
386
386
 
387
387
  end
388
388
 
389
+
390
+
389
391
  ```
390
392
 
391
393
  **【部分テンプレート】**
@@ -400,7 +402,7 @@
400
402
 
401
403
  <%= form_for(current_user.relationships.find_by(follow_id: user.id), html: { method: :delete }) do |f| %>
402
404
 
403
- <%= f.hidden_field :follow_id, user.id %>
405
+ <%= hidden_field_tag :follow_id, user.id %>
404
406
 
405
407
  <%= f.submit 'Unfollow', class: 'btn btn-danger btn-block' %>
406
408
 
@@ -410,7 +412,7 @@
410
412
 
411
413
  <%= form_for(current_user.relationships.build) do |f| %>
412
414
 
413
- <%= f.hidden_field :follow_id, user.id %>
415
+ <%= hidden_field_tag :follow_id, user.id %>
414
416
 
415
417
  <%= f.submit 'Follow', class: 'btn btn-primary btn-block' %>
416
418
 

6

relationships_controller,_follow_button.html.erbの記述変更

2020/01/16 09:32

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -202,7 +202,7 @@
202
202
 
203
203
  def create
204
204
 
205
- user = User.find(params[:relationship][:follow_id])
205
+ @user = User.find(params[:relationship][:follow_id])
206
206
 
207
207
  following = current_user.follow(user)
208
208
 
@@ -210,13 +210,13 @@
210
210
 
211
211
  flash[:success] = 'ユーザーをフォローしました'
212
212
 
213
- redirect_to user
213
+ redirect_to @user
214
214
 
215
215
  else
216
216
 
217
217
  flash.now[:alert] = 'ユーザーのフォローに失敗しました'
218
218
 
219
- redirect_to user
219
+ redirect_to @user
220
220
 
221
221
  end
222
222
 
@@ -226,7 +226,7 @@
226
226
 
227
227
  def destroy
228
228
 
229
- user = User.find(params[:relationship][:follow_id])
229
+ @user = User.find(params[:relationship][:follow_id])
230
230
 
231
231
  following = current_user.unfollow(user)
232
232
 
@@ -234,13 +234,13 @@
234
234
 
235
235
  flash[:success] = 'ユーザーのフォローを解除しました'
236
236
 
237
- redirect_to user
237
+ redirect_to @user
238
238
 
239
239
  else
240
240
 
241
241
  flash.now[:alert] = 'ユーザーのフォロー解除に失敗しました'
242
242
 
243
- redirect_to user
243
+ redirect_to @user
244
244
 
245
245
  end
246
246
 
@@ -254,7 +254,7 @@
254
254
 
255
255
  def set_user
256
256
 
257
- user = User.find(params[:relationship][:follow_id])
257
+ @user = User.find(params[:relationship][:follow_id])
258
258
 
259
259
  end
260
260
 
@@ -400,7 +400,7 @@
400
400
 
401
401
  <%= form_for(current_user.relationships.find_by(follow_id: user.id), html: { method: :delete }) do |f| %>
402
402
 
403
- <%= hidden_field_tag :follow_id, user.id %>
403
+ <%= f.hidden_field :follow_id, user.id %>
404
404
 
405
405
  <%= f.submit 'Unfollow', class: 'btn btn-danger btn-block' %>
406
406
 
@@ -410,7 +410,7 @@
410
410
 
411
411
  <%= form_for(current_user.relationships.build) do |f| %>
412
412
 
413
- <%= hidden_field_tag :follow_id, user.id %>
413
+ <%= f.hidden_field :follow_id, user.id %>
414
414
 
415
415
  <%= f.submit 'Follow', class: 'btn btn-primary btn-block' %>
416
416
 

5

indexアクションでの表記変更

2020/01/16 09:14

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -294,7 +294,7 @@
294
294
 
295
295
  @post_images = PostImage.page(params[:page]).reverse_order
296
296
 
297
- @user = PostImage.find(params[:id])
297
+ @user = @post_image.id
298
298
 
299
299
  end
300
300
 

4

PostImage.rbの追加

2020/01/16 08:39

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -154,6 +154,40 @@
154
154
 
155
155
  ```
156
156
 
157
+ post_image.rb
158
+
159
+ ```
160
+
161
+ class PostImage < ApplicationRecord
162
+
163
+ belongs_to :user
164
+
165
+
166
+
167
+ has_many :favorites, dependent: :destroy
168
+
169
+ has_many :fav_users, through: :favorites, source: :user
170
+
171
+ has_many :cosplay_favorites, dependent: :destroy
172
+
173
+ has_many :cosplay_fav_users, through: :cosplay_favorites, source: :user
174
+
175
+ has_many :post_comments, dependent: :destroy
176
+
177
+
178
+
179
+ attachment :real_image
180
+
181
+ attachment :cosplay_image
182
+
183
+
184
+
185
+ default_scope -> { order(created_at: :asc) }
186
+
187
+ end
188
+
189
+ ```
190
+
157
191
  【Controller】
158
192
 
159
193
  **relationship_controller.rb**

3

users_controller.rbの追加

2020/01/16 03:00

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -304,6 +304,56 @@
304
304
 
305
305
  ```
306
306
 
307
+ **users_controller.rb**
308
+
309
+ ```
310
+
311
+ class UsersController < ApplicationController
312
+
313
+ def show
314
+
315
+ @user = User.find(params[:id])
316
+
317
+ @post_images = @user.post_images.page(params[:page]).reverse_order
318
+
319
+ end
320
+
321
+
322
+
323
+ def edit
324
+
325
+ @user = User.find(params[:id])
326
+
327
+ end
328
+
329
+
330
+
331
+ def update
332
+
333
+ @user = User.finr(params[:id])
334
+
335
+ @user.update(user_params)
336
+
337
+ redirect_to user_path(@user.id)
338
+
339
+ end
340
+
341
+
342
+
343
+ private
344
+
345
+
346
+
347
+ def user_params
348
+
349
+ params.requrie(:user).permit(:name, :profile_image)
350
+
351
+ end
352
+
353
+ end
354
+
355
+ ```
356
+
307
357
  **【部分テンプレート】**
308
358
 
309
359
  **relationships/_follow_button.html.erb**

2

コントローラーの追記

2020/01/16 00:54

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -234,15 +234,13 @@
234
234
 
235
235
  class PostImagesController < ApplicationController
236
236
 
237
-
238
-
239
- def new
237
+ def new
240
-
238
+
241
- @post_image = PostImage.new
239
+ @post_image = PostImage.new
242
-
240
+
243
- end
241
+ end
244
-
245
-
242
+
243
+
246
244
 
247
245
  def create
248
246
 
@@ -250,62 +248,60 @@
250
248
 
251
249
  @post_image.user_id = current_user.id
252
250
 
253
- if @post_image.save
251
+ @post_image.save
254
-
252
+
255
- redirect_to post_images_path
253
+ redirect_to post_images_path
256
-
257
- else
254
+
258
-
259
- render :new
260
-
261
- end
255
+ end
262
-
263
- end
256
+
264
-
265
-
257
+
266
258
 
267
259
  def index
268
260
 
269
- @post_images = PostImage.page(params[:page]).reverse_order
261
+ @post_images = PostImage.page(params[:page]).reverse_order
262
+
270
-
263
+ @user = PostImage.find(params[:id])
264
+
271
- end
265
+ end
272
-
273
-
266
+
267
+
274
268
 
275
269
  def show
276
270
 
277
- @post_image = PostImage.find(params[:id])
271
+ @post_image = PostImage.find(params[:id])
278
-
272
+
279
- @post_comment = PostComment.new
273
+ @post_comment = PostComment.new
280
-
274
+
281
- end
275
+ end
282
-
283
-
284
-
276
+
277
+
278
+
285
- def destroy
279
+ def destroy
286
-
280
+
287
- @post_image = PostImage.find(params[:id])
281
+ @post_image = PostImage.find(params[:id])
288
-
282
+
289
- @post_image.destroy
283
+ @post_image.destroy
290
-
284
+
291
- redirect_to post_images_path
285
+ redirect_to post_images_path
292
-
286
+
293
- end
287
+ end
294
-
295
-
288
+
289
+
296
290
 
297
291
  private
298
292
 
299
-
293
+
300
294
 
301
295
  def post_image_params
302
296
 
303
- params.require(:post_image).permit(:shop_name, :image, :caption)
297
+ params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption, :favorites_count)
304
298
 
305
299
  end
306
300
 
307
301
  end
308
302
 
303
+
304
+
309
305
  ```
310
306
 
311
307
  **【部分テンプレート】**

1

コントローラーの追加

2020/01/16 00:52

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -228,6 +228,86 @@
228
228
 
229
229
  ```
230
230
 
231
+ **postimages_controller.rb**
232
+
233
+ ```
234
+
235
+ class PostImagesController < ApplicationController
236
+
237
+
238
+
239
+ def new
240
+
241
+ @post_image = PostImage.new
242
+
243
+ end
244
+
245
+
246
+
247
+ def create
248
+
249
+ @post_image = PostImage.new(post_image_params)
250
+
251
+ @post_image.user_id = current_user.id
252
+
253
+ if @post_image.save
254
+
255
+ redirect_to post_images_path
256
+
257
+ else
258
+
259
+ render :new
260
+
261
+ end
262
+
263
+ end
264
+
265
+
266
+
267
+ def index
268
+
269
+ @post_images = PostImage.page(params[:page]).reverse_order
270
+
271
+ end
272
+
273
+
274
+
275
+ def show
276
+
277
+ @post_image = PostImage.find(params[:id])
278
+
279
+ @post_comment = PostComment.new
280
+
281
+ end
282
+
283
+
284
+
285
+ def destroy
286
+
287
+ @post_image = PostImage.find(params[:id])
288
+
289
+ @post_image.destroy
290
+
291
+ redirect_to post_images_path
292
+
293
+ end
294
+
295
+
296
+
297
+ private
298
+
299
+
300
+
301
+ def post_image_params
302
+
303
+ params.require(:post_image).permit(:shop_name, :image, :caption)
304
+
305
+ end
306
+
307
+ end
308
+
309
+ ```
310
+
231
311
  **【部分テンプレート】**
232
312
 
233
313
  **relationships/_follow_button.html.erb**