質問編集履歴

1

質問の意図を追記

2017/07/03 07:12

投稿

anvinon
anvinon

スコア38

test CHANGED
File without changes
test CHANGED
@@ -23,3 +23,517 @@
23
23
  他にデータベースへ保存する方法があればご教示ください。
24
24
 
25
25
  よろしくお願いします。
26
+
27
+
28
+
29
+
30
+
31
+ **※以下追記**
32
+
33
+
34
+
35
+ 現在ECサイトを開発しております。各商品(product)のページに複数のレビュー(review)を投稿して、複数のコメント(comment)が出来るようにアソシエーションを組みました。そして、commentsテーブルにreview_idカラムを作成し、reviewsテーブルのidをreview_idに保存したいのですが、コントローラーやビューで試行錯誤しても、うまくいかないため、質問をさせて頂きました。下記にソースコードを掲載致しました。
36
+
37
+
38
+
39
+
40
+
41
+ ビューは以下のとおりです。
42
+
43
+ ```Ruby
44
+
45
+ # app/views/products/show.html.erb
46
+
47
+
48
+
49
+ <h1>商品詳細</h1>
50
+
51
+ <%= @product.price %>
52
+
53
+ <%= @product.description %>
54
+
55
+ <%= image_tag @product.image.url %>
56
+
57
+
58
+
59
+ <%= form_tag(:controller => :products, :action => :add) do %>
60
+
61
+ <%= select_tag :item, options_for_select(["01","02","03","04","05"]) %>
62
+
63
+ <%= submit_tag "Add Cart" %>
64
+
65
+ <% end %>
66
+
67
+
68
+
69
+ <%= link_to '編集', "/products/#{@product.id}/edit", method: :get %>
70
+
71
+ <%= link_to '削除', "/products/#{@product.id}", method: :delete %>
72
+
73
+ <% if session[:user_id] %>
74
+
75
+ <h2>レビュー投稿</h2>
76
+
77
+ <%= form_for(@review_new, :url => {:controller => :reviews, :action => :create}) do |f| %>
78
+
79
+ <%= f.text_area :text %>
80
+
81
+ <%= f.hidden_field :user_id, :value => "#{session[:user_id]}" %>
82
+
83
+ <%= f.hidden_field :name, :value => "#{session[:name]}" %>
84
+
85
+ <%= f.hidden_field :product_id, :value => "#{@product.id}" %>
86
+
87
+ <%= f.submit %>
88
+
89
+ <% end %>
90
+
91
+ <% end %>
92
+
93
+ <h2>ユーザーレビュー</h2>
94
+
95
+ <% if @reviews.nil? %>
96
+
97
+ まだユーザーレビューはありません。
98
+
99
+ <% else %>
100
+
101
+ <% @reviews.each do |r| %>
102
+
103
+ ユーザー名:<%= r.name %>
104
+
105
+ レビュー文:<%= r.text %>
106
+
107
+ <%= link_to 'コメントする', "/comments/new", method: :get %>
108
+
109
+ <% r.comments.each do |c| %>
110
+
111
+ コメント:<%= c.text %>
112
+
113
+ <% end %>
114
+
115
+ <% end %>
116
+
117
+ <% end %>
118
+
119
+ ```
120
+
121
+
122
+
123
+ ```Ruby
124
+
125
+ # app/views/comments/new.html.erb
126
+
127
+
128
+
129
+ <h1>コメント投稿</h1>
130
+
131
+ <%= form_for(@comment_new, :url => {:controller => :comments, :action => :create}) do |f| %>
132
+
133
+ <%= f.text_area :text %>
134
+
135
+ <%= f.hidden_field :user_id, :value => session[:user_id] %>
136
+
137
+ <%= f.hidden_field :name, :value => session[:name] %>
138
+
139
+ <%= f.hidden_field :product_id, :value => session[:product_show_id] %>
140
+
141
+ <%= f.submit %>
142
+
143
+ <% end %>
144
+
145
+ ```
146
+
147
+ コントローラーは以下のとおりです。
148
+
149
+ ```Ruby
150
+
151
+ # app/controllers/products_controller.rb
152
+
153
+
154
+
155
+ class ProductsController < ApplicationController
156
+
157
+
158
+
159
+ def index
160
+
161
+ @product_all = Product.all
162
+
163
+ end
164
+
165
+
166
+
167
+ def new
168
+
169
+ @product_new = Product.new
170
+
171
+ @review =Review.new
172
+
173
+ end
174
+
175
+
176
+
177
+ def create
178
+
179
+ Product.create(product_params_create)
180
+
181
+ redirect_to :action => "index"
182
+
183
+ end
184
+
185
+
186
+
187
+ def show
188
+
189
+ @product = Product.find(params[:id])
190
+
191
+ session[:cart] ||= {}
192
+
193
+ session[:cart]["#{params[:id]}"] = Product.find(params[:id])
194
+
195
+ @review_new = Review.new
196
+
197
+ session[:product_show_id] = params[:id]
198
+
199
+ @comment_all = Comment.all
200
+
201
+ @reviews = @product.reviews
202
+
203
+ end
204
+
205
+
206
+
207
+ def add
208
+
209
+ if session[:item] == nil then
210
+
211
+ session[:item] ||= {}
212
+
213
+ session[:item]["#{params[:id]}"] = params[:item]
214
+
215
+ else
216
+
217
+ session[:item]["#{params[:id]}"] = params[:item].to_i + session[:item]["#{params[:id]}"].to_i
218
+
219
+ end
220
+
221
+ end
222
+
223
+
224
+
225
+
226
+
227
+ def content
228
+
229
+ end
230
+
231
+
232
+
233
+ def destroy
234
+
235
+ product = Product.find(params[:id])
236
+
237
+ product.destroy
238
+
239
+ end
240
+
241
+
242
+
243
+ def edit
244
+
245
+ @product_edit = Product.find(params[:id])
246
+
247
+ end
248
+
249
+
250
+
251
+ def update
252
+
253
+ @product_update = Product.find(params[:id])
254
+
255
+ @product_update.update(product_params_update)
256
+
257
+ redirect_to :action => "index"
258
+
259
+ end
260
+
261
+
262
+
263
+ def search
264
+
265
+ @products = Product.all
266
+
267
+ if params[:description].present?
268
+
269
+ @products = @products.get_by_description params[:description]
270
+
271
+ end
272
+
273
+ end
274
+
275
+
276
+
277
+ private
278
+
279
+ def product_params_create
280
+
281
+ params.require(:product).permit(:price, :description, :image)
282
+
283
+ end
284
+
285
+
286
+
287
+ def product_params_update
288
+
289
+ params.permit(:price, :description, :image)
290
+
291
+ end
292
+
293
+
294
+
295
+ end
296
+
297
+ ```
298
+
299
+
300
+
301
+ ```Ruby
302
+
303
+ # app/controllers/reviews_controller.rb
304
+
305
+
306
+
307
+ class ReviewsController < ApplicationController
308
+
309
+
310
+
311
+ def index
312
+
313
+ @reviews = Review.all
314
+
315
+ end
316
+
317
+
318
+
319
+ def new
320
+
321
+ @review_new = Review.new
322
+
323
+ end
324
+
325
+
326
+
327
+ def create
328
+
329
+ review = Review.create(review_params_create)
330
+
331
+ redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}"
332
+
333
+ end
334
+
335
+
336
+
337
+ def edit
338
+
339
+ @review_edit = Review.find(params[:id])
340
+
341
+ end
342
+
343
+
344
+
345
+ def update
346
+
347
+ @review_update = Review.find(params[:id])
348
+
349
+ @review_update.update(review_params_update)
350
+
351
+ redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}"
352
+
353
+ end
354
+
355
+
356
+
357
+ def destroy
358
+
359
+ Review.find(params[:id]).destroy
360
+
361
+ redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}"
362
+
363
+ end
364
+
365
+
366
+
367
+ private
368
+
369
+ def review_params_create
370
+
371
+ params.require(:review).permit(:text, :user_id, :name, :product_id)
372
+
373
+ end
374
+
375
+
376
+
377
+ def review_params_update
378
+
379
+ params.require(:review).permit(:text)
380
+
381
+ end
382
+
383
+ end
384
+
385
+ ```
386
+
387
+
388
+
389
+ ```Ruby
390
+
391
+ # app/controllers/comments_controller.rb
392
+
393
+
394
+
395
+ class CommentsController < ApplicationController
396
+
397
+
398
+
399
+ def index
400
+
401
+ @comment_index = Comment.all
402
+
403
+ end
404
+
405
+
406
+
407
+ def new
408
+
409
+ @comment_new = Comment.new
410
+
411
+ end
412
+
413
+
414
+
415
+ def create
416
+
417
+ session[:comment] = Comment.create(comment_params_create)
418
+
419
+ email = User.find(session[:user_id]).email
420
+
421
+ name = session[:name]
422
+
423
+ url = ""
424
+
425
+ if ENV["RAILS_ENV"] == "development"
426
+
427
+ url = "http://localhost:3000/products/#{session[:product_id]}"
428
+
429
+ else
430
+
431
+ url = "https://slup-app.herokuapp.com/products/#{session[:product_id]}"
432
+
433
+ end
434
+
435
+ NoticeNewComment.notice_new_comment(name, url, email).deliver_now
436
+
437
+ redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}"
438
+
439
+ end
440
+
441
+
442
+
443
+ private
444
+
445
+ def comment_params_create
446
+
447
+ params.require(:comment).permit(:text, :user_id, :review_id)
448
+
449
+ end
450
+
451
+ end
452
+
453
+ ```
454
+
455
+
456
+
457
+ モデルは以下のとおりです。
458
+
459
+ ```Ruby
460
+
461
+ # app/models/comment.rb
462
+
463
+
464
+
465
+ class Comment < ActiveRecord::Base
466
+
467
+ belongs_to :user
468
+
469
+ belongs_to :review
470
+
471
+ end
472
+
473
+ ```
474
+
475
+
476
+
477
+ ```Ruby
478
+
479
+ # app/models/product.rb
480
+
481
+
482
+
483
+ class Product < ActiveRecord::Base
484
+
485
+ mount_uploader :image, ImageUploader
486
+
487
+ #ユーザー名による絞り込み
488
+
489
+ scope :get_by_description, ->(description) {where("description like ?", "%#{description}%")}
490
+
491
+ has_many :reviews
492
+
493
+ end
494
+
495
+ ```
496
+
497
+
498
+
499
+ ```Ruby
500
+
501
+ # app/models/review.rb
502
+
503
+
504
+
505
+ class Review < ActiveRecord::Base
506
+
507
+ belongs_to :user
508
+
509
+ belongs_to :product
510
+
511
+ has_many :comments
512
+
513
+ end
514
+
515
+ ```
516
+
517
+
518
+
519
+ ```Ruby
520
+
521
+ # app/models/user.rb
522
+
523
+
524
+
525
+ class User < ActiveRecord::Base
526
+
527
+ has_secure_password
528
+
529
+ validates :name, presence: true, :uniqueness => true
530
+
531
+ validates :email, presence: true, :uniqueness => true
532
+
533
+ has_many :reviews
534
+
535
+ has_many :comments
536
+
537
+ end
538
+
539
+ ```