質問編集履歴

4

修正点の編集

2020/01/13 05:43

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -76,6 +76,8 @@
76
76
 
77
77
  t.integer :user_id
78
78
 
79
+ t.integer :favorites_count
80
+
79
81
 
80
82
 
81
83
  t.timestamps
@@ -312,7 +314,7 @@
312
314
 
313
315
  def set_variables
314
316
 
315
- @post_image = PostImage.find(params[:post_image_id])
317
+ @post_image = PostImage.find(params[:id])
316
318
 
317
319
  @id_name = "#favorite-link-#{@post_image.id}"
318
320
 
@@ -372,7 +374,7 @@
372
374
 
373
375
  def post_image_params
374
376
 
375
- params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption)
377
+ params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption, :favorites_count)
376
378
 
377
379
  end
378
380
 
@@ -502,7 +504,7 @@
502
504
 
503
505
 
504
506
 
505
- <%= render partial: 'post_images/post_images', collection: @post_images, as: :post_image %>
507
+ <%= render partial: 'post_images/post_images', locals: { post_image: post_image } %>
506
508
 
507
509
 
508
510
 
@@ -514,14 +516,6 @@
514
516
 
515
517
  ####**部分テンプレート**
516
518
 
517
- ######views/post_images/index.html.erb
518
-
519
- ```
520
-
521
- <%= render partial: 'post_images/post_images', collection: @post_images, as: :post_image %>
522
-
523
- ```
524
-
525
519
  ######views/post_images/_post_images.html.erb
526
520
 
527
521
  ```

3

マイグレーションファイルの追加

2020/01/13 05:43

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -26,6 +26,8 @@
26
26
 
27
27
  ### 該当のソースコード
28
28
 
29
+ ####マイグレーションファイル
30
+
29
31
  ######**favoritesテーブル(20200112070415_create_favorites.rb)**
30
32
 
31
33
  ```
@@ -52,6 +54,134 @@
52
54
 
53
55
  ```
54
56
 
57
+ ######post_imagesテーブル
58
+
59
+ ```
60
+
61
+ class CreatePostImages < ActiveRecord::Migration[5.2]
62
+
63
+ def change
64
+
65
+ create_table :post_images do |t|
66
+
67
+ t.text :real_image_name
68
+
69
+ t.text :cosplay_image_name
70
+
71
+ t.string :real_image_id
72
+
73
+ t.string :cosplay_image_id
74
+
75
+ t.text :caption
76
+
77
+ t.integer :user_id
78
+
79
+
80
+
81
+ t.timestamps
82
+
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ ```
90
+
91
+ ######deviseユーザーテーブル
92
+
93
+ ```
94
+
95
+ # frozen_string_literal: true
96
+
97
+
98
+
99
+ class DeviseCreateUsers < ActiveRecord::Migration[5.2]
100
+
101
+ def change
102
+
103
+ create_table :users do |t|
104
+
105
+ ## Database authenticatable
106
+
107
+ t.string :email, null: false, default: ""
108
+
109
+ t.string :encrypted_password, null: false, default: ""
110
+
111
+
112
+
113
+ ## Recoverable
114
+
115
+ t.string :reset_password_token
116
+
117
+ t.datetime :reset_password_sent_at
118
+
119
+
120
+
121
+ ## Rememberable
122
+
123
+ t.datetime :remember_created_at
124
+
125
+
126
+
127
+ ## Trackable
128
+
129
+ # t.integer :sign_in_count, default: 0, null: false
130
+
131
+ # t.datetime :current_sign_in_at
132
+
133
+ # t.datetime :last_sign_in_at
134
+
135
+ # t.string :current_sign_in_ip
136
+
137
+ # t.string :last_sign_in_ip
138
+
139
+
140
+
141
+ ## Confirmable
142
+
143
+ # t.string :confirmation_token
144
+
145
+ # t.datetime :confirmed_at
146
+
147
+ # t.datetime :confirmation_sent_at
148
+
149
+ # t.string :unconfirmed_email # Only if using reconfirmable
150
+
151
+
152
+
153
+ ## Lockable
154
+
155
+ # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
156
+
157
+ # t.string :unlock_token # Only if unlock strategy is :email or :both
158
+
159
+ # t.datetime :locked_at
160
+
161
+
162
+
163
+ t.string :name
164
+
165
+ t.timestamps null: false
166
+
167
+ end
168
+
169
+
170
+
171
+ add_index :users, :email, unique: true
172
+
173
+ add_index :users, :reset_password_token, unique: true
174
+
175
+ # add_index :users, :confirmation_token, unique: true
176
+
177
+ # add_index :users, :unlock_token, unique: true
178
+
179
+ end
180
+
181
+ end
182
+
183
+ ```
184
+
55
185
  ####**モデル**
56
186
 
57
187
  ######favorite.rb

2

ビュー、コントローラーの追加

2020/01/12 13:35

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -256,12 +256,8 @@
256
256
 
257
257
  class ApplicationController < ActionController::Base
258
258
 
259
- # deviseの機能が使われる場合、その前にconfigure_permitted_parametersが実行される
260
-
261
259
  before_action :configure_permitted_parameters, if: :devise_controller?
262
260
 
263
- # ログインユーザーのみ操作を許可する
264
-
265
261
  before_action :authenticate_user!
266
262
 
267
263
 
@@ -270,8 +266,6 @@
270
266
 
271
267
 
272
268
 
273
- # ユーザー登録の際に、nameの操作が許可される
274
-
275
269
  def configure_permitted_parameters
276
270
 
277
271
  devise_parameter_sanitizer.permit(:sign_up, keys: [:name])

1

ビュー、コントローラーの追加

2020/01/12 13:30

投稿

is02
is02

スコア17

test CHANGED
File without changes
test CHANGED
@@ -192,6 +192,202 @@
192
192
 
193
193
  ```
194
194
 
195
+ ######post_images_controller.rb
196
+
197
+ ```
198
+
199
+ class PostImagesController < ApplicationController
200
+
201
+ def new
202
+
203
+ @post_image = PostImage.new
204
+
205
+ end
206
+
207
+
208
+
209
+ def create
210
+
211
+ @post_image = PostImage.new(post_image_params)
212
+
213
+ @post_image.user_id = current_user.id
214
+
215
+ @post_image.save
216
+
217
+ redirect_to post_images_path
218
+
219
+ end
220
+
221
+
222
+
223
+ def index
224
+
225
+ @post_images = PostImage.all
226
+
227
+ end
228
+
229
+
230
+
231
+ def show
232
+
233
+ @post_image = PostImage.find(params[:id])
234
+
235
+ end
236
+
237
+
238
+
239
+ private
240
+
241
+
242
+
243
+ def post_image_params
244
+
245
+ params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption)
246
+
247
+ end
248
+
249
+ end
250
+
251
+ ```
252
+
253
+ ######application_controller.rb
254
+
255
+ ```
256
+
257
+ class ApplicationController < ActionController::Base
258
+
259
+ # deviseの機能が使われる場合、その前にconfigure_permitted_parametersが実行される
260
+
261
+ before_action :configure_permitted_parameters, if: :devise_controller?
262
+
263
+ # ログインユーザーのみ操作を許可する
264
+
265
+ before_action :authenticate_user!
266
+
267
+
268
+
269
+ protected
270
+
271
+
272
+
273
+ # ユーザー登録の際に、nameの操作が許可される
274
+
275
+ def configure_permitted_parameters
276
+
277
+ devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
278
+
279
+ end
280
+
281
+ end
282
+
283
+ ```
284
+
285
+ ####ビュー
286
+
287
+ ######postimages/index.html.erb
288
+
289
+ ```
290
+
291
+ <div class="header">
292
+
293
+ <nav class="navigation">
294
+
295
+ <img src="/assets/logo.png">
296
+
297
+ <ul>
298
+
299
+ <li>
300
+
301
+ <%= link_to "ログアウト", destroy_user_session_path, method: :delete %>
302
+
303
+ </li>
304
+
305
+
306
+
307
+ <li>
308
+
309
+ <%= link_to '投稿する', new_post_image_path %>
310
+
311
+ </li>
312
+
313
+
314
+
315
+ <li>
316
+
317
+ <%= link_to 'マイページ', '#' %>
318
+
319
+ </li>
320
+
321
+ </ul>
322
+
323
+ </nav>
324
+
325
+ </div>
326
+
327
+ <div class="post_images_index_wrapper">
328
+
329
+ <% @post_images.each do |post_image| %>
330
+
331
+ <div class="post_images_index_user">
332
+
333
+ <ul>
334
+
335
+ <li><%= image_tag('no_image.jpg') %></li>
336
+
337
+ <li><p><%= post_image.user.name %></p></li>
338
+
339
+ </ul>
340
+
341
+ </div>
342
+
343
+ <div class="post_images_box">
344
+
345
+ <div class="post_image">
346
+
347
+ <%= attachment_image_tag post_image, :real_image %>
348
+
349
+ </div>
350
+
351
+ <div class="post_image">
352
+
353
+ <%= attachment_image_tag post_image, :cosplay_image %>
354
+
355
+ </div>
356
+
357
+ </div>
358
+
359
+ <h3 class="block-title">
360
+
361
+ <%= link_to post_image_path(post_image.id) do %>
362
+
363
+ <%= post_image.real_image_name %>
364
+
365
+ <% end %>
366
+
367
+ <%= link_to post_image_path(post_image.id) do %>
368
+
369
+ <%= post_image.cosplay_image_name %>
370
+
371
+ <% end %>
372
+
373
+ </h3>
374
+
375
+ <p><%= post_image.caption %></p>
376
+
377
+ <a href="#">23 コメント</a>
378
+
379
+
380
+
381
+ <%= render partial: 'post_images/post_images', collection: @post_images, as: :post_image %>
382
+
383
+
384
+
385
+ <% end %>
386
+
387
+ </div>
388
+
389
+ ```
390
+
195
391
  ####**部分テンプレート**
196
392
 
197
393
  ######views/post_images/index.html.erb
@@ -202,7 +398,7 @@
202
398
 
203
399
  ```
204
400
 
205
- views/post_images/_post_images.html.erb
401
+ ######views/post_images/_post_images.html.erb
206
402
 
207
403
  ```
208
404
 
@@ -210,7 +406,7 @@
210
406
 
211
407
  ```
212
408
 
213
- views/favorites/_favorite.html.erb
409
+ ######views/favorites/_favorite.html.erb
214
410
 
215
411
  ```
216
412