質問編集履歴
2
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -60,108 +60,138 @@
|
|
60
60
|
|
61
61
|
def show
|
62
62
|
|
63
|
+
@products = Product.all
|
64
|
+
|
65
|
+
if @product == @products.first
|
66
|
+
|
67
|
+
@next_product = Product.next_search(@product)
|
68
|
+
|
69
|
+
elsif @product == @products.last
|
70
|
+
|
71
|
+
@prev_product = Product.prev_search(@product)
|
72
|
+
|
73
|
+
else
|
74
|
+
|
75
|
+
@prev_product = Product.prev_search(@product)
|
76
|
+
|
77
|
+
@next_product = Product.next_search(@product)
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
@grandchild = Category.find(@product.category_id)
|
82
|
+
|
83
|
+
@child = @grandchild.parent
|
84
|
+
|
85
|
+
@parent = @child.parent
|
86
|
+
|
87
|
+
@parent_category_products = @products.select { |product| product.category.root.name == @parent.name }
|
88
|
+
|
89
|
+
@user = current_user
|
90
|
+
|
91
|
+
Payjp.api_key = ENV['PAYJP_PRIVATE_KEY']
|
92
|
+
|
93
|
+
@card = CreditCard.find_by(user_id: current_user)
|
94
|
+
|
95
|
+
if user_signed_in?
|
96
|
+
|
97
|
+
@favorite = Favorite.find_by(user_id: current_user.id, product_id: params[:id])
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
```favorites
|
106
|
+
|
107
|
+
class FavoritesController < ApplicationController
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
def create
|
112
|
+
|
113
|
+
@favorite = Favorite.create(user_id: current_user.id, product_id: params[:product_id])
|
114
|
+
|
115
|
+
@favorites = Favorite.where(product_id: params[:product_id])
|
116
|
+
|
117
|
+
@product = Product.find(params[:product_id])
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
def destroy
|
124
|
+
|
125
|
+
@favorite = Favorite.find_by(user_id: current_user.id, product_id: params[:product_id])
|
126
|
+
|
127
|
+
@favorite.destroy
|
128
|
+
|
129
|
+
@favorites = Favorite.where(product_id: params[:product_id])
|
130
|
+
|
131
|
+
@product = Product.find(params[:product_id])
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
・モデル
|
140
|
+
|
141
|
+
```product
|
142
|
+
|
63
143
|
中略
|
64
144
|
|
145
|
+
has_many :favorites, dependent: :destroy
|
146
|
+
|
147
|
+
has_many :favorite_users, through: :favorites, source: :user
|
148
|
+
|
149
|
+
|
150
|
+
|
65
|
-
|
151
|
+
def favorite_user(user_id)
|
66
|
-
|
152
|
+
|
67
|
-
|
153
|
+
favorites.find_by(user_id: user_id)
|
68
|
-
|
154
|
+
|
69
|
-
end
|
155
|
+
end
|
156
|
+
|
157
|
+
```
|
158
|
+
|
159
|
+
```user
|
160
|
+
|
161
|
+
中略
|
162
|
+
|
163
|
+
has_many :products, dependent: :destroy
|
164
|
+
|
165
|
+
has_many :favorites, dependent: :destroy
|
166
|
+
|
167
|
+
has_many :favorite_products, through: :favorites, source: :product
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
def already_favorited?(product)
|
172
|
+
|
173
|
+
self.favorites.exists?(product_id: product.id)
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
```
|
178
|
+
|
179
|
+
```favorite
|
180
|
+
|
181
|
+
class Favorite < ApplicationRecord
|
182
|
+
|
183
|
+
belongs_to :user
|
184
|
+
|
185
|
+
belongs_to :product
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
validates_uniqueness_of :product_id, scope: :user_id
|
70
190
|
|
71
191
|
end
|
72
192
|
|
73
193
|
```
|
74
194
|
|
75
|
-
```favorites
|
76
|
-
|
77
|
-
class FavoritesController < ApplicationController
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
def create
|
82
|
-
|
83
|
-
@favorite = Favorite.create(user_id: current_user.id, product_id: params[:product_id])
|
84
|
-
|
85
|
-
@favorites = Favorite.where(product_id: params[:product_id])
|
86
|
-
|
87
|
-
@product = Product.find(params[:product_id])
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
def destroy
|
94
|
-
|
95
|
-
@favorite = Favorite.find_by(user_id: current_user.id, product_id: params[:product_id])
|
96
|
-
|
97
|
-
@favorite.destroy
|
98
|
-
|
99
|
-
@favorites = Favorite.where(product_id: params[:product_id])
|
100
|
-
|
101
|
-
@product = Product.find(params[:product_id])
|
102
|
-
|
103
|
-
end
|
104
|
-
|
105
|
-
end
|
106
|
-
|
107
|
-
```
|
108
|
-
|
109
|
-
・モデル
|
110
|
-
|
111
|
-
```product
|
112
|
-
|
113
|
-
中略
|
114
|
-
|
115
|
-
has_many :favorites, dependent: :destroy
|
116
|
-
|
117
|
-
has_many :favorite_users, through: :favorites, source: :user
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
def favorite_user(user_id)
|
122
|
-
|
123
|
-
favorites.find_by(user_id: user_id)
|
124
|
-
|
125
|
-
end
|
126
|
-
|
127
|
-
```
|
128
|
-
|
129
|
-
```user
|
130
|
-
|
131
|
-
中略
|
132
|
-
|
133
|
-
has_many :products, dependent: :destroy
|
134
|
-
|
135
|
-
has_many :favorites, dependent: :destroy
|
136
|
-
|
137
|
-
has_many :favorite_products, through: :favorites, source: :product
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
def already_favorited?(product)
|
142
|
-
|
143
|
-
self.favorites.exists?(product_id: product.id)
|
144
|
-
|
145
|
-
end
|
146
|
-
|
147
|
-
```
|
148
|
-
|
149
|
-
```favorite
|
150
|
-
|
151
|
-
class Favorite < ApplicationRecord
|
152
|
-
|
153
|
-
belongs_to :user
|
154
|
-
|
155
|
-
belongs_to :product
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
validates_uniqueness_of :product_id, scope: :user_id
|
160
|
-
|
161
|
-
end
|
162
|
-
|
163
|
-
```
|
164
|
-
|
165
195
|
・ビュー
|
166
196
|
|
167
197
|
products/index.html.haml
|
@@ -192,14 +222,48 @@
|
|
192
222
|
|
193
223
|
- if user_signed_in?
|
194
224
|
|
225
|
+
- if current_user.already_favorited?(product)
|
226
|
+
|
227
|
+
= link_to product_favorite_path(product, @favorite), method: :delete, remote: true do
|
228
|
+
|
229
|
+
%i.fas.fa-star
|
230
|
+
|
231
|
+
お気に入り
|
232
|
+
|
233
|
+
= product.favorites.count
|
234
|
+
|
235
|
+
- else
|
236
|
+
|
237
|
+
= link_to product_favorites_path(product), method: :post, remote: true do
|
238
|
+
|
239
|
+
%i.far.fa-star
|
240
|
+
|
241
|
+
お気に入り
|
242
|
+
|
243
|
+
= product.favorites.count
|
244
|
+
|
245
|
+
- else
|
246
|
+
|
247
|
+
= icon('fa', 'star')
|
248
|
+
|
249
|
+
お気に入り
|
250
|
+
|
251
|
+
= product.favorites.count
|
252
|
+
|
253
|
+
```
|
254
|
+
|
255
|
+
favorites/_products_favorite.html.haml
|
256
|
+
|
257
|
+
```
|
258
|
+
|
259
|
+
- if user_signed_in?
|
260
|
+
|
195
261
|
- if current_user.already_favorited?(@product)
|
196
262
|
|
197
263
|
= link_to product_favorite_path(@product, @favorite), method: :delete, remote: true do
|
198
264
|
|
199
265
|
%i.fas.fa-star
|
200
266
|
|
201
|
-
お気に入り
|
202
|
-
|
203
267
|
= @product.favorites.count
|
204
268
|
|
205
269
|
- else
|
@@ -208,50 +272,16 @@
|
|
208
272
|
|
209
273
|
%i.far.fa-star
|
210
274
|
|
211
|
-
お気に入り
|
212
|
-
|
213
275
|
= @product.favorites.count
|
214
276
|
|
215
277
|
- else
|
216
278
|
|
217
279
|
= icon('fa', 'star')
|
218
280
|
|
219
|
-
お気に入り
|
220
|
-
|
221
281
|
= @product.favorites.count
|
222
282
|
|
223
283
|
```
|
224
284
|
|
225
|
-
favorites/_products_favorite.html.haml
|
226
|
-
|
227
|
-
```
|
228
|
-
|
229
|
-
- if user_signed_in?
|
230
|
-
|
231
|
-
- if current_user.already_favorited?(@product)
|
232
|
-
|
233
|
-
= link_to product_favorite_path(@product, @favorite), method: :delete, remote: true do
|
234
|
-
|
235
|
-
%i.fas.fa-star
|
236
|
-
|
237
|
-
= @product.favorites.count
|
238
|
-
|
239
|
-
- else
|
240
|
-
|
241
|
-
= link_to product_favorites_path(@product), method: :post, remote: true do
|
242
|
-
|
243
|
-
%i.far.fa-star
|
244
|
-
|
245
|
-
= @product.favorites.count
|
246
|
-
|
247
|
-
- else
|
248
|
-
|
249
|
-
= icon('fa', 'star')
|
250
|
-
|
251
|
-
= @product.favorites.count
|
252
|
-
|
253
|
-
```
|
254
|
-
|
255
285
|
・jsファイル
|
256
286
|
|
257
287
|
favorites/create.js.haml、destroy.js.haml
|
1
実装機能のタイトルの追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
NoMethodError undefined method `id' for nil:NilClass
|
1
|
+
いいね機能 NoMethodError undefined method `id' for nil:NilClass
|
test
CHANGED
File without changes
|