質問編集履歴

2

誤字

2020/04/11 05:56

投稿

yastinbieber
yastinbieber

スコア49

test CHANGED
File without changes
test CHANGED
@@ -6,9 +6,9 @@
6
6
 
7
7
  ### 具体的には
8
8
 
9
- 現在productsコントローラのshowビューファイルをuserコントローラのshowビューファイル中で表示したいです。
9
+ productsコントローラのshowビューファイルをuserコントローラのshowビューファイル中で表示したいです。
10
10
 
11
- イメージとしては、自身が投稿したプロダクトに対するレビューを陳列する形です。
11
+ イメージとしては、自身が投稿したレビューをusers/show.html.erbに陳列する形です。
12
12
 
13
13
 
14
14
 

1

文法の修正

2020/04/11 05:56

投稿

yastinbieber
yastinbieber

スコア49

test CHANGED
@@ -1 +1 @@
1
- Userページにおける他テーブルの部分テンプレートを反映させたい
1
+ Rails 部分テンプレート 他コントローラーのビュー表示(userコントローラ)
test CHANGED
@@ -1,64 +1,36 @@
1
- ### 前提・実現したいこと
1
+ ## 実現したいこと
2
2
 
3
- user_showページにprouct_showの一(部分テンプレート反映させたい
3
+ Rails部分テンプレートを使い、他コントローラのビューを表示したいです。
4
4
 
5
5
 
6
6
 
7
- ### 該当のソースコード
7
+ ### 具体的には
8
+
9
+ 現在productsコントローラのshowビューファイルをuserコントローラのshowビューファイル中で表示したいです。
10
+
11
+ イメージとしては、自身が投稿したプロダクトに対するレビューを陳列する形です。
8
12
 
9
13
 
10
14
 
11
- ```Rails
15
+ ##該当のソースコード
12
16
 
17
+ ```
18
+
13
- ##user_showページ
19
+ ##products/show.html.erb
14
20
 
15
21
 
16
22
 
17
- <div class="contents-box">
23
+ <% @products.each do |review| %>
18
24
 
19
- <div class="container-left">
25
+ <%= render partial: "product", locals: { product: @product, review: review } %>
20
26
 
21
- --<省略>--
22
-
23
- <% @users.each do |review| %>
24
-
25
- ** !!<ここに部分テンプレートを採用したい>!!**
26
-
27
- <% end %>
27
+ <% end %>
28
-
29
- <div class="JIP">
30
-
31
- <%= paginate(@users) %>
32
-
33
- </div>
34
-
35
- </div>
36
-
37
- </div>
38
28
 
39
29
  ```
40
30
 
41
-
42
-
43
- ※下記部分テンプレートをuser_showにも適応させたい。
44
-
45
- ```Rails
46
-
47
- ##product_showページ
48
-
49
-
50
-
51
- <% @products.each do |review| %>
52
-
53
- <%= render partial: "product", locals: { product: @product, review: review } %>
54
-
55
- <% end %>
56
-
57
31
  ```
58
32
 
59
- ```Rails
60
-
61
- ##_product.html.erb (参考までに繰り返し部分)
33
+ ##products/_product.html.erb
62
34
 
63
35
 
64
36
 
@@ -74,7 +46,7 @@
74
46
 
75
47
  <% if review.comments.present? %>
76
48
 
77
- <span>レビューに対しての平均評価:</span>
49
+ <span>@<%= review.user.nickname %>のレビューに対しての平均評価:</span>
78
50
 
79
51
  <span class="rating-star">
80
52
 
@@ -84,7 +56,7 @@
84
56
 
85
57
  <% else %>
86
58
 
87
- <span>レビューに対しての平均評価:</span>
59
+ <span>@<%= review.user.nickname %>のレビューに対しての平均評価:</span>
88
60
 
89
61
  <span class="rating-star">
90
62
 
@@ -120,41 +92,31 @@
120
92
 
121
93
  ```
122
94
 
95
+ 上記内容を下記(users/show.html.erb)のeach文の間に設定したいです。
123
96
 
97
+ ```
124
98
 
125
- コントローラも念の為貼っておきます。
126
-
127
- ```Rails
128
-
129
- ##products_controller
99
+ ##users/show.html.erb
130
100
 
131
101
 
132
102
 
133
- class ProductsController < ApplicationController
103
+ <% @users.each do |review| %>
134
104
 
135
- before_action :authenticate_user!
105
+ <ーーここにーー>
136
106
 
137
-
138
-
139
- def show
107
+ <% end %>
140
-
141
- @product = Product.find(params[:id])
142
-
143
- @review = @product.reviews.all.count
144
-
145
- @products = @product.reviews.page(params[:page]).per(3).order("created_at DESC")
146
-
147
- end
148
-
149
- end
150
108
 
151
109
  ```
152
110
 
153
- ```rails
154
-
155
- ##user_controller
156
111
 
157
112
 
113
+ 念の為usersコントローラも載せておきます。
114
+
115
+ ```
116
+
117
+
118
+
119
+ ##users_controller
158
120
 
159
121
  class UsersController < ApplicationController
160
122
 
@@ -174,106 +136,26 @@
174
136
 
175
137
 
176
138
 
177
- モデルも貼っておきます。
178
-
179
- ```Rails
139
+ ## 試したこと
180
-
181
- ##product_rb
182
-
183
-
184
-
185
- class Product < ApplicationRecord
186
-
187
-
188
-
189
- has_one_attached :image
190
-
191
-
192
-
193
- has_many :reviews
194
-
195
- belongs_to :user
196
-
197
- has_many :comments
198
-
199
-
200
-
201
- end
202
-
203
-
204
140
 
205
141
  ```
206
142
 
207
- ```rails
208
-
209
- ##user_rb
210
-
211
-
212
-
213
- class User < ApplicationRecord
214
-
215
- # Include default devise modules. Others available are:
216
-
217
- # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
218
-
219
-
220
-
221
- has_one_attached :image
222
-
223
-
224
-
225
- devise :database_authenticatable, :registerable,
226
-
227
- :recoverable, :rememberable, :validatable, :confirmable
228
-
229
-
230
-
231
- has_many :products
232
-
233
- has_many :reviews
234
-
235
- has_many :comments
236
-
237
-
238
-
239
- validates_presence_of :image, :nickname, :selfintroduction
240
-
241
-
242
-
243
- def full_profile?
244
-
245
- image.attached? && nickname? && selfintroduction?
246
-
247
- end
248
-
249
-
250
-
251
- end
252
-
253
-
254
-
255
- ```
256
-
257
-
258
-
259
-
260
-
261
- ### 試したこと
262
-
263
-
264
-
265
- ```Rails
266
-
267
143
  <% @users.each do |review| %>
268
144
 
269
- <%= render partial: "products/product", locals: { product: @product, review: review } %>
145
+ <%= render partial: "products/show", locals: { product: @product, review: review } %>
270
146
 
271
147
  <% end %>
272
148
 
273
149
  ```
274
150
 
275
- 他テーブルを反映させるため、partial部分を変えてみましたがエラーが出てしまい進めせんでした。
151
+ ようなエラーが出てしまいました。
152
+
153
+ https://gyazo.com/586d83de03b84db3e727815de751cac0
276
154
 
277
155
 
278
156
 
157
+ :as => "review"を最後尾に加えたりもしましたが、SyntaxErrorが出ます。。
158
+
159
+
160
+
279
- お手数おけしすが、わかる方ご教示いただけますと幸いです。
161
+ どなたくいく法をご教示いただけますと幸いです。