質問編集履歴

1

追記

2020/05/28 03:35

投稿

yastinbieber
yastinbieber

スコア49

test CHANGED
File without changes
test CHANGED
@@ -28,45 +28,183 @@
28
28
 
29
29
 
30
30
 
31
-
31
+ ※該当コードは下記にまとめて記述しております。
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
- ### 該当のソースコード
41
+ ###binding.pryすると
42
+
42
-
43
+ ```ここに言語を入力
44
+
43
-
45
+ From: /home/ec2-user/environment/bodymake/app/controllers/fooddiaries_controller.rb:21 FooddiariesController#create:
46
+
47
+
48
+
44
-
49
+ 19: def create
50
+
51
+ 20: @fooddiary = current_user.fooddiaries.create(fooddiary_params)
52
+
53
+ => 21: binding.pry
54
+
55
+ 22: redirect_to action: 'index'
56
+
57
+ 23: end
58
+
59
+
60
+
61
+ [1] pry(#<FooddiariesController>)> @fooddiary.errors.messages
62
+
63
+ => {:idealweight=>["must exist"], :idealweight_id=>["can't be blank"]}
64
+
65
+ ```
66
+
67
+
68
+
69
+ ### 補足
70
+
45
- ```Rails
71
+ ```rails
72
+
73
+ ##fooddiaries_controller.rb
74
+
75
+
76
+
77
+ class FooddiariesController < ApplicationController
78
+
79
+
80
+
81
+ def new
82
+
83
+ @fooddiary = current_user.fooddiaries.new
84
+
85
+ end
86
+
87
+
88
+
89
+ def create
90
+
91
+ @fooddiary = current_user.fooddiaries.create(fooddiary_params)
92
+
93
+ redirect_to action: 'index'
94
+
95
+ end
96
+
97
+
98
+
99
+ private
100
+
101
+ def fooddiary_params
102
+
103
+ params.require(:fooddiary).permit(:gram, :kcal, :protein, :fat, :carbo, :timing, :foodname, :fooddate_id, :user_id, :idealweight_id)
104
+
105
+ end
106
+
107
+
108
+
109
+ end
110
+
111
+
112
+
113
+ ```
114
+
115
+ ```rails
116
+
117
+ ##fooddiaries/new.html.erb
118
+
119
+
120
+
121
+ <%= form_for @fooddiary do |f| %>
122
+
123
+ <tbody>
124
+
125
+ <tr>
126
+
127
+ <th>foodname</th>
128
+
129
+ <th>gram</th>
130
+
131
+ <th>timing</th>
132
+
133
+ </tr><br>
134
+
135
+ <%= f.collection_select :fooddate_id, Fooddate.all, :id, :foodname %>
136
+
137
+ <td><%= f.number_field :gram, placeholder: "gram" %></td>
138
+
139
+ <td><%= f.select :timing, ['朝食', '昼食', '夕食', '間食'] %></td>
140
+
141
+ <br><%= f.submit "送信", class: "btn" %>
142
+
143
+ </tbody>
144
+
145
+ <% end %>
146
+
147
+
148
+
149
+ ```
150
+
151
+ ```rails
152
+
153
+ class Idealweight < ApplicationRecord
46
154
 
47
155
  ##idealweight.rb
48
156
 
49
157
 
50
158
 
51
- class Idealweight < ApplicationRecord
52
-
53
159
  belongs_to :user
54
160
 
55
161
  has_many :fooddiaries
56
162
 
163
+
164
+
165
+ before_save do
166
+
167
+ self.basemetabolism = 13.397*(weight)+4.799*(height)-5.677*25+88.362
168
+
169
+ self.kcalburned = (basemetabolism)*(exerciselevel)
170
+
171
+ self.minusweight = (weight)-(targetweight)
172
+
173
+ self.minuskcal = (minusweight)*7000
174
+
175
+ self.kcalintake = (kcalburned)-(minuskcal)/(period)
176
+
177
+ self.intakeproteingram = (weight)*2
178
+
179
+ self.intakeproteinkcal = (weight)*2*4
180
+
181
+ self.intakefatgram = (weight)*0.7
182
+
183
+ self.intakefatkcal = (weight)*0.7*9
184
+
185
+ self.intakecarbokcal = (kcalintake)-(intakeproteinkcal)-(intakefatkcal)
186
+
187
+ self.intakecarbogram = (intakecarbokcal)/4
188
+
189
+ end
190
+
57
191
  end
58
192
 
193
+
194
+
59
- ```
195
+ ```
60
-
196
+
61
- ```Rails
197
+ ```rails
62
198
 
63
199
  ##fooddiary.rb
64
200
 
65
-
66
-
67
- class Fooddiary < ApplicationRecord
201
+ class Fooddiary < ApplicationRecord
202
+
203
+
204
+
68
-
205
+ belongs_to :fooddate
69
-
206
+
207
+
70
208
 
71
209
  belongs_to :user
72
210
 
@@ -80,40 +218,34 @@
80
218
 
81
219
 
82
220
 
221
+ def self.search(search)
222
+
223
+ if search
224
+
225
+ where(['created_at LIKE ?', "%#{search}%"])
226
+
227
+ end
228
+
229
+ end
230
+
231
+
232
+
233
+ before_save do
234
+
235
+ self.kcal = (gram)*(fooddate.kcal)
236
+
237
+ self.protein = (gram)*(fooddate.protein)
238
+
239
+ self.fat = (gram)*(fooddate.fat)
240
+
241
+ self.carbo = (gram)*(fooddate.carbo)
242
+
243
+ end
244
+
245
+
246
+
83
247
  end
84
248
 
85
249
 
86
250
 
87
251
  ```
88
-
89
-
90
-
91
- ###binding.pryすると
92
-
93
- ```ここに言語を入力
94
-
95
- From: /home/ec2-user/environment/bodymake/app/controllers/fooddiaries_controller.rb:22 FooddiariesController#create:
96
-
97
-
98
-
99
- 19: def create
100
-
101
- 20: @fooddiaries = current_user.fooddiaries.create(fooddiary_params)
102
-
103
- 21: redirect_to action: 'index'
104
-
105
- => 22: binding.pry
106
-
107
- 23: end
108
-
109
-
110
-
111
- [1] pry(#<FooddiariesController>)> params
112
-
113
- => <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"HHqFX9dnCtq2E5Ce8Kh66Fb1zS9gnu8T5VfqAvyX3zw/xMNBfwC6HAKUPIHRLsXrmtIrXSxdDcicUYKA/KId4A==", "fooddiary"=><ActionController::Parameters {"fooddate_id"=>"1", "gram"=>"100", "timing"=>"夕食"} permitted: false>, "commit"=>"送信", "controller"=>"fooddiaries", "action"=>"create"} permitted: false>
114
-
115
- ```
116
-
117
-
118
-
119
- よろしくお願いいたします。