質問編集履歴

1

コードを全文記載しました。

2020/10/15 16:03

投稿

tarotarotarotar
tarotarotarotar

スコア41

test CHANGED
File without changes
test CHANGED
@@ -76,19 +76,131 @@
76
76
 
77
77
  ```ここに言語を入力
78
78
 
79
- 中略
80
-
81
- <% if !Favorite.exists?(user_id:current_user.id, nutrition_id:nutrition.id) %>
82
-
83
- <%= link_to "登録", user_favorites_path(user_id:current_user.id, nutrition_id:nutrition.id), method: :post %>
79
+ <%= form_with(url: search_nutritions_path, local: true, method: :get, class: "search-form") do |form| %>
84
-
85
- <% else %>
80
+
86
-
87
- <%= link_to "解除", favorite_path(user_id:current_user.id, nutrition_id:nutrition.id), method: :delete %>
81
+ <%= form.text_field :keyword, placeholder: "食品名を入力", class: "search-input" %>
82
+
83
+ <%= form.submit "Search", class: "search-btn" %>
88
84
 
89
85
  <% end %>
90
86
 
87
+
88
+
89
+ <script src="delete.js"></script>
90
+
91
+
92
+
93
+ <div class='main-contents'>
94
+
95
+
96
+
97
+ <h1 class="contents-title">食品一覧(100gあたり)</h1>
98
+
99
+ <table border="5" width="80%" cellpadding="20" bordercolor="#882d91" class="contents-column" align="left">
100
+
101
+ <tr class="column-name">
102
+
103
+ <th width="30%" height="50">食品名</th>
104
+
105
+ <th class="column-color1" width="15%">エネルギー(kcal)</th>
106
+
107
+ <th class="column-color1" width="15%">タンパク質(g)</th>
108
+
109
+ <th class="column-color1" width="15%">脂質(g)</th>
110
+
111
+ <th class="column-color1" width="15%">炭水化物(g)</th>
112
+
113
+ <th class="column-color2" width="15%">カリウム(mg)</th>
114
+
115
+ <th class="column-color2" width="15%">カルシウム(mg)</th>
116
+
117
+ <th class="column-color2" width="15%">鉄(mg)</th>
118
+
119
+ <th class="column-color3" width="15%">ビタミンA(mg)</th>
120
+
121
+ <th class="column-color3" width="15%">ビタミンB1(mg)</th>
122
+
123
+ <th class="column-color3" width="15%">ビタミンB2(mg)</th>
124
+
125
+ <th class="column-color3" width="15%">ビタミンC(mg)</th>
126
+
127
+ <th width="15%">食塩相当量(g)</th>
128
+
129
+ <th width="15%">登録ユーザー</th>
130
+
131
+ </tr>
132
+
133
+
134
+
135
+ <% @nutritions.each do |nutrition| %>
136
+
137
+ <tr height="60" class="content-post">
138
+
139
+ <td class="ingredient-column" id="ingredient-column"><%= nutrition.ingredient %>
140
+
141
+ <div class="more" id="more">
142
+
143
+ <ul class="more-list" id="more-list">
144
+
91
- 中略
145
+ <li>
146
+
147
+ <% if current_user.already_favorited?(nutrition) %>
148
+
149
+ <%= link_to '解除', nutrition_favorite_path(id:favorite.id, user_id:current_user.id, nutrition_id:nutrition.id), method: :delete %>
150
+
151
+ <% else %>
152
+
153
+ <%= link_to '登録', edit_nutrition_path(nutrition.id), method: :get %>
154
+
155
+ <% end %>
156
+
157
+ <%= link_to '編集', edit_nutrition_path(nutrition.id), method: :get %>
158
+
159
+ <%= link_to '削除', nutrition_path(nutrition.id), method: :delete, data: { confirm: '削除しますか?'} %>
160
+
161
+ </li>
162
+
163
+ </ul>
164
+
165
+ </div>
166
+
167
+ </td>
168
+
169
+ <td><%= nutrition.calorie %></td>
170
+
171
+ <td><%= nutrition.protein %></td>
172
+
173
+ <td><%= nutrition.lipid %></td>
174
+
175
+ <td><%= nutrition.carbohydrate %></td>
176
+
177
+ <td><%= nutrition.potassium %></td>
178
+
179
+ <td><%= nutrition.calcium %></td>
180
+
181
+ <td><%= nutrition.iron %></td>
182
+
183
+ <td><%= nutrition.vitamin_a %></td>
184
+
185
+ <td><%= nutrition.vitamin_b1 %></td>
186
+
187
+ <td><%= nutrition.vitamin_b2 %></td>
188
+
189
+ <td><%= nutrition.vitamin_c %></td>
190
+
191
+ <td><%= nutrition.salt_equivalent %></td>
192
+
193
+ <td><%= nutrition.user.nickname %></td>
194
+
195
+ </tr>
196
+
197
+ <% end %>
198
+
199
+ </table>
200
+
201
+ </div>
202
+
203
+
92
204
 
93
205
  ```
94
206
 
@@ -98,7 +210,35 @@
98
210
 
99
211
  ```ここに言語を入力
100
212
 
213
+ Rails.application.routes.draw do
214
+
215
+ devise_for :users
216
+
217
+ # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
218
+
219
+ root to: 'nutritions#index'
220
+
221
+ resources :nutritions do
222
+
223
+ collection do
224
+
225
+ get 'search'
226
+
101
- 中略
227
+ end
228
+
229
+ member do
230
+
231
+ post "add", to: "favorites#create"
232
+
233
+ end
234
+
235
+
236
+
237
+ resource :favorites, only: [:destroy]
238
+
239
+ end
240
+
241
+
102
242
 
103
243
  resources :users do
104
244
 
@@ -106,11 +246,9 @@
106
246
 
107
247
  end
108
248
 
109
-
110
-
111
- resources :favorites, only: [:destroy]
112
-
113
- 中略
249
+ end
250
+
251
+
114
252
 
115
253
  ```
116
254
 
@@ -120,11 +258,43 @@
120
258
 
121
259
  ```ここに言語を入力
122
260
 
261
+ class FavoritesController < ApplicationController
262
+
263
+
264
+
265
+ def show
266
+
267
+ @nutritions = current_user.nutritions
268
+
269
+ @user = User.find(params[:id])
270
+
271
+ @favorites = Favorite.where("user_id = ?", @user)
272
+
123
- 中略
273
+ end
274
+
275
+
276
+
277
+ def create
278
+
279
+ @user_id = current_user.id
280
+
281
+ @nutrition_id = Nutrition.find(params[:nutrition_id])
282
+
283
+ @favorite = Favorite.new(nutrition_id: @nutrition_id.id, user_id: @user_id)
284
+
285
+ if @favorite.save!
286
+
287
+ redirect_to root_path
288
+
289
+ end
290
+
291
+ end
292
+
293
+
124
294
 
125
295
  def destroy
126
296
 
127
- @favorite = Favorite.find_by(user_id:current_user.id, nutrition_id:nutrition.id)
297
+ @favorite = Favorite.find_by(user_id:current_user.id, nutrition_id:params[:nutrition_id])
128
298
 
129
299
  if @favorite.destroy
130
300
 
@@ -146,7 +316,11 @@
146
316
 
147
317
  end
148
318
 
319
+
320
+
149
- 中略
321
+ end
322
+
323
+
150
324
 
151
325
 
152
326