teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

質問の意図を追記

2017/07/03 07:12

投稿

anvinon
anvinon

スコア38

title CHANGED
File without changes
body CHANGED
@@ -10,4 +10,261 @@
10
10
  1. フォームに入力した内容(パラーメータ)をコントローラーで Hoge.create(パラメータ) を使ってデータベースに保存
11
11
 
12
12
  他にデータベースへ保存する方法があればご教示ください。
13
- よろしくお願いします。
13
+ よろしくお願いします。
14
+
15
+
16
+ **※以下追記**
17
+
18
+ 現在ECサイトを開発しております。各商品(product)のページに複数のレビュー(review)を投稿して、複数のコメント(comment)が出来るようにアソシエーションを組みました。そして、commentsテーブルにreview_idカラムを作成し、reviewsテーブルのidをreview_idに保存したいのですが、コントローラーやビューで試行錯誤しても、うまくいかないため、質問をさせて頂きました。下記にソースコードを掲載致しました。
19
+
20
+
21
+ ビューは以下のとおりです。
22
+ ```Ruby
23
+ # app/views/products/show.html.erb
24
+
25
+ <h1>商品詳細</h1>
26
+ <%= @product.price %>
27
+ <%= @product.description %>
28
+ <%= image_tag @product.image.url %>
29
+
30
+ <%= form_tag(:controller => :products, :action => :add) do %>
31
+ <%= select_tag :item, options_for_select(["01","02","03","04","05"]) %>
32
+ <%= submit_tag "Add Cart" %>
33
+ <% end %>
34
+
35
+ <%= link_to '編集', "/products/#{@product.id}/edit", method: :get %>
36
+ <%= link_to '削除', "/products/#{@product.id}", method: :delete %>
37
+ <% if session[:user_id] %>
38
+ <h2>レビュー投稿</h2>
39
+ <%= form_for(@review_new, :url => {:controller => :reviews, :action => :create}) do |f| %>
40
+ <%= f.text_area :text %>
41
+ <%= f.hidden_field :user_id, :value => "#{session[:user_id]}" %>
42
+ <%= f.hidden_field :name, :value => "#{session[:name]}" %>
43
+ <%= f.hidden_field :product_id, :value => "#{@product.id}" %>
44
+ <%= f.submit %>
45
+ <% end %>
46
+ <% end %>
47
+ <h2>ユーザーレビュー</h2>
48
+ <% if @reviews.nil? %>
49
+ まだユーザーレビューはありません。
50
+ <% else %>
51
+ <% @reviews.each do |r| %>
52
+ ユーザー名:<%= r.name %>
53
+ レビュー文:<%= r.text %>
54
+ <%= link_to 'コメントする', "/comments/new", method: :get %>
55
+ <% r.comments.each do |c| %>
56
+ コメント:<%= c.text %>
57
+ <% end %>
58
+ <% end %>
59
+ <% end %>
60
+ ```
61
+
62
+ ```Ruby
63
+ # app/views/comments/new.html.erb
64
+
65
+ <h1>コメント投稿</h1>
66
+ <%= form_for(@comment_new, :url => {:controller => :comments, :action => :create}) do |f| %>
67
+ <%= f.text_area :text %>
68
+ <%= f.hidden_field :user_id, :value => session[:user_id] %>
69
+ <%= f.hidden_field :name, :value => session[:name] %>
70
+ <%= f.hidden_field :product_id, :value => session[:product_show_id] %>
71
+ <%= f.submit %>
72
+ <% end %>
73
+ ```
74
+ コントローラーは以下のとおりです。
75
+ ```Ruby
76
+ # app/controllers/products_controller.rb
77
+
78
+ class ProductsController < ApplicationController
79
+
80
+ def index
81
+ @product_all = Product.all
82
+ end
83
+
84
+ def new
85
+ @product_new = Product.new
86
+ @review =Review.new
87
+ end
88
+
89
+ def create
90
+ Product.create(product_params_create)
91
+ redirect_to :action => "index"
92
+ end
93
+
94
+ def show
95
+ @product = Product.find(params[:id])
96
+ session[:cart] ||= {}
97
+ session[:cart]["#{params[:id]}"] = Product.find(params[:id])
98
+ @review_new = Review.new
99
+ session[:product_show_id] = params[:id]
100
+ @comment_all = Comment.all
101
+ @reviews = @product.reviews
102
+ end
103
+
104
+ def add
105
+ if session[:item] == nil then
106
+ session[:item] ||= {}
107
+ session[:item]["#{params[:id]}"] = params[:item]
108
+ else
109
+ session[:item]["#{params[:id]}"] = params[:item].to_i + session[:item]["#{params[:id]}"].to_i
110
+ end
111
+ end
112
+
113
+
114
+ def content
115
+ end
116
+
117
+ def destroy
118
+ product = Product.find(params[:id])
119
+ product.destroy
120
+ end
121
+
122
+ def edit
123
+ @product_edit = Product.find(params[:id])
124
+ end
125
+
126
+ def update
127
+ @product_update = Product.find(params[:id])
128
+ @product_update.update(product_params_update)
129
+ redirect_to :action => "index"
130
+ end
131
+
132
+ def search
133
+ @products = Product.all
134
+ if params[:description].present?
135
+ @products = @products.get_by_description params[:description]
136
+ end
137
+ end
138
+
139
+ private
140
+ def product_params_create
141
+ params.require(:product).permit(:price, :description, :image)
142
+ end
143
+
144
+ def product_params_update
145
+ params.permit(:price, :description, :image)
146
+ end
147
+
148
+ end
149
+ ```
150
+
151
+ ```Ruby
152
+ # app/controllers/reviews_controller.rb
153
+
154
+ class ReviewsController < ApplicationController
155
+
156
+ def index
157
+ @reviews = Review.all
158
+ end
159
+
160
+ def new
161
+ @review_new = Review.new
162
+ end
163
+
164
+ def create
165
+ review = Review.create(review_params_create)
166
+ redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}"
167
+ end
168
+
169
+ def edit
170
+ @review_edit = Review.find(params[:id])
171
+ end
172
+
173
+ def update
174
+ @review_update = Review.find(params[:id])
175
+ @review_update.update(review_params_update)
176
+ redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}"
177
+ end
178
+
179
+ def destroy
180
+ Review.find(params[:id]).destroy
181
+ redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}"
182
+ end
183
+
184
+ private
185
+ def review_params_create
186
+ params.require(:review).permit(:text, :user_id, :name, :product_id)
187
+ end
188
+
189
+ def review_params_update
190
+ params.require(:review).permit(:text)
191
+ end
192
+ end
193
+ ```
194
+
195
+ ```Ruby
196
+ # app/controllers/comments_controller.rb
197
+
198
+ class CommentsController < ApplicationController
199
+
200
+ def index
201
+ @comment_index = Comment.all
202
+ end
203
+
204
+ def new
205
+ @comment_new = Comment.new
206
+ end
207
+
208
+ def create
209
+ session[:comment] = Comment.create(comment_params_create)
210
+ email = User.find(session[:user_id]).email
211
+ name = session[:name]
212
+ url = ""
213
+ if ENV["RAILS_ENV"] == "development"
214
+ url = "http://localhost:3000/products/#{session[:product_id]}"
215
+ else
216
+ url = "https://slup-app.herokuapp.com/products/#{session[:product_id]}"
217
+ end
218
+ NoticeNewComment.notice_new_comment(name, url, email).deliver_now
219
+ redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}"
220
+ end
221
+
222
+ private
223
+ def comment_params_create
224
+ params.require(:comment).permit(:text, :user_id, :review_id)
225
+ end
226
+ end
227
+ ```
228
+
229
+ モデルは以下のとおりです。
230
+ ```Ruby
231
+ # app/models/comment.rb
232
+
233
+ class Comment < ActiveRecord::Base
234
+ belongs_to :user
235
+ belongs_to :review
236
+ end
237
+ ```
238
+
239
+ ```Ruby
240
+ # app/models/product.rb
241
+
242
+ class Product < ActiveRecord::Base
243
+ mount_uploader :image, ImageUploader
244
+ #ユーザー名による絞り込み
245
+ scope :get_by_description, ->(description) {where("description like ?", "%#{description}%")}
246
+ has_many :reviews
247
+ end
248
+ ```
249
+
250
+ ```Ruby
251
+ # app/models/review.rb
252
+
253
+ class Review < ActiveRecord::Base
254
+ belongs_to :user
255
+ belongs_to :product
256
+ has_many :comments
257
+ end
258
+ ```
259
+
260
+ ```Ruby
261
+ # app/models/user.rb
262
+
263
+ class User < ActiveRecord::Base
264
+ has_secure_password
265
+ validates :name, presence: true, :uniqueness => true
266
+ validates :email, presence: true, :uniqueness => true
267
+ has_many :reviews
268
+ has_many :comments
269
+ end
270
+ ```