質問編集履歴
1
コードを全文記載しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -37,31 +37,116 @@
|
|
37
37
|
【ビュー】index.html.erb
|
38
38
|
登録されていれば解除ボタン表示、登録されていなければ登録ボタンを表示
|
39
39
|
```ここに言語を入力
|
40
|
-
中略
|
41
|
-
<% if !Favorite.exists?(user_id:current_user.id, nutrition_id:nutrition.id) %>
|
42
|
-
|
40
|
+
<%= form_with(url: search_nutritions_path, local: true, method: :get, class: "search-form") do |form| %>
|
43
|
-
<% else %>
|
44
|
-
<%=
|
41
|
+
<%= form.text_field :keyword, placeholder: "食品名を入力", class: "search-input" %>
|
42
|
+
<%= form.submit "Search", class: "search-btn" %>
|
45
43
|
<% end %>
|
44
|
+
|
45
|
+
<script src="delete.js"></script>
|
46
|
+
|
47
|
+
<div class='main-contents'>
|
48
|
+
|
49
|
+
<h1 class="contents-title">食品一覧(100gあたり)</h1>
|
50
|
+
<table border="5" width="80%" cellpadding="20" bordercolor="#882d91" class="contents-column" align="left">
|
51
|
+
<tr class="column-name">
|
52
|
+
<th width="30%" height="50">食品名</th>
|
53
|
+
<th class="column-color1" width="15%">エネルギー(kcal)</th>
|
54
|
+
<th class="column-color1" width="15%">タンパク質(g)</th>
|
55
|
+
<th class="column-color1" width="15%">脂質(g)</th>
|
56
|
+
<th class="column-color1" width="15%">炭水化物(g)</th>
|
57
|
+
<th class="column-color2" width="15%">カリウム(mg)</th>
|
58
|
+
<th class="column-color2" width="15%">カルシウム(mg)</th>
|
59
|
+
<th class="column-color2" width="15%">鉄(mg)</th>
|
60
|
+
<th class="column-color3" width="15%">ビタミンA(mg)</th>
|
61
|
+
<th class="column-color3" width="15%">ビタミンB1(mg)</th>
|
62
|
+
<th class="column-color3" width="15%">ビタミンB2(mg)</th>
|
63
|
+
<th class="column-color3" width="15%">ビタミンC(mg)</th>
|
64
|
+
<th width="15%">食塩相当量(g)</th>
|
65
|
+
<th width="15%">登録ユーザー</th>
|
66
|
+
</tr>
|
67
|
+
|
68
|
+
<% @nutritions.each do |nutrition| %>
|
69
|
+
<tr height="60" class="content-post">
|
70
|
+
<td class="ingredient-column" id="ingredient-column"><%= nutrition.ingredient %>
|
71
|
+
<div class="more" id="more">
|
72
|
+
<ul class="more-list" id="more-list">
|
46
|
-
|
73
|
+
<li>
|
74
|
+
<% if current_user.already_favorited?(nutrition) %>
|
75
|
+
<%= link_to '解除', nutrition_favorite_path(id:favorite.id, user_id:current_user.id, nutrition_id:nutrition.id), method: :delete %>
|
76
|
+
<% else %>
|
77
|
+
<%= link_to '登録', edit_nutrition_path(nutrition.id), method: :get %>
|
78
|
+
<% end %>
|
79
|
+
<%= link_to '編集', edit_nutrition_path(nutrition.id), method: :get %>
|
80
|
+
<%= link_to '削除', nutrition_path(nutrition.id), method: :delete, data: { confirm: '削除しますか?'} %>
|
81
|
+
</li>
|
82
|
+
</ul>
|
83
|
+
</div>
|
84
|
+
</td>
|
85
|
+
<td><%= nutrition.calorie %></td>
|
86
|
+
<td><%= nutrition.protein %></td>
|
87
|
+
<td><%= nutrition.lipid %></td>
|
88
|
+
<td><%= nutrition.carbohydrate %></td>
|
89
|
+
<td><%= nutrition.potassium %></td>
|
90
|
+
<td><%= nutrition.calcium %></td>
|
91
|
+
<td><%= nutrition.iron %></td>
|
92
|
+
<td><%= nutrition.vitamin_a %></td>
|
93
|
+
<td><%= nutrition.vitamin_b1 %></td>
|
94
|
+
<td><%= nutrition.vitamin_b2 %></td>
|
95
|
+
<td><%= nutrition.vitamin_c %></td>
|
96
|
+
<td><%= nutrition.salt_equivalent %></td>
|
97
|
+
<td><%= nutrition.user.nickname %></td>
|
98
|
+
</tr>
|
99
|
+
<% end %>
|
100
|
+
</table>
|
101
|
+
</div>
|
102
|
+
|
47
103
|
```
|
48
104
|
|
49
105
|
【ルーティング】routes.rb
|
50
106
|
```ここに言語を入力
|
107
|
+
Rails.application.routes.draw do
|
108
|
+
devise_for :users
|
109
|
+
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
|
110
|
+
root to: 'nutritions#index'
|
111
|
+
resources :nutritions do
|
112
|
+
collection do
|
113
|
+
get 'search'
|
51
|
-
|
114
|
+
end
|
115
|
+
member do
|
116
|
+
post "add", to: "favorites#create"
|
117
|
+
end
|
118
|
+
|
119
|
+
resource :favorites, only: [:destroy]
|
120
|
+
end
|
121
|
+
|
52
122
|
resources :users do
|
53
123
|
resources :favorites, only: [:show, :create]
|
54
124
|
end
|
125
|
+
end
|
55
126
|
|
56
|
-
resources :favorites, only: [:destroy]
|
57
|
-
中略
|
58
127
|
```
|
59
128
|
|
60
129
|
【コントローラー】favorites_controller.rb
|
61
130
|
```ここに言語を入力
|
131
|
+
class FavoritesController < ApplicationController
|
132
|
+
|
133
|
+
def show
|
134
|
+
@nutritions = current_user.nutritions
|
135
|
+
@user = User.find(params[:id])
|
136
|
+
@favorites = Favorite.where("user_id = ?", @user)
|
62
|
-
|
137
|
+
end
|
138
|
+
|
139
|
+
def create
|
140
|
+
@user_id = current_user.id
|
141
|
+
@nutrition_id = Nutrition.find(params[:nutrition_id])
|
142
|
+
@favorite = Favorite.new(nutrition_id: @nutrition_id.id, user_id: @user_id)
|
143
|
+
if @favorite.save!
|
144
|
+
redirect_to root_path
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
63
148
|
def destroy
|
64
|
-
@favorite = Favorite.find_by(user_id:current_user.id, nutrition_id:
|
149
|
+
@favorite = Favorite.find_by(user_id:current_user.id, nutrition_id:params[:nutrition_id])
|
65
150
|
if @favorite.destroy
|
66
151
|
redirect_to root_path
|
67
152
|
end
|
@@ -72,9 +157,11 @@
|
|
72
157
|
def favorite_params
|
73
158
|
params.require(:favorite).permit(:user_id, :nutrition_id)
|
74
159
|
end
|
75
|
-
中略
|
76
160
|
|
161
|
+
end
|
77
162
|
|
163
|
+
|
164
|
+
|
78
165
|
```
|
79
166
|
|
80
167
|
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
|