質問編集履歴
2
修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
タイプエラーが解決できない
|
body
CHANGED
@@ -1,205 +1,45 @@
|
|
1
|
-
|
1
|
+
require 'rails_helper'
|
2
|
-
以下のエラーが解消できません。
|
3
2
|
|
4
|
-
bundle exec rspec spec/requests/nutritions_spec.rbの実行結果
|
5
|
-
|
6
|
-
``` 1) NutritionsController GET #index indexアクションにリクエストするとレスポンスに登録済みのカロリーが存在する
|
7
|
-
Failure/Error: expect(response.body).to include @nutrition.calorie
|
8
|
-
|
9
|
-
TypeError:
|
10
|
-
no implicit conversion of Float into String
|
11
|
-
# ./spec/requests/nutritions_spec.rb:21:in `block (3 levels) in <top (required)>'
|
12
|
-
|
13
|
-
コード
|
14
|
-
```
|
15
|
-
nutritionsコントローラーというもののテストコードを記述しております。その際に以下のようなテストコードを書いております。そのうち、「indexアクションにリクエストするとレスポンスに登録済みの食品名が存在する」「indexアクションにリクエストするとレスポンスに登録済みのカロリーが存在する」にて同一のエラー分が返ってきます。
|
16
|
-
|
17
|
-
【nutritions_spec.rb】
|
18
|
-
```require 'rails_helper'
|
19
|
-
describe
|
3
|
+
RSpec.describe 'ユーザー新規登録', type: :system do
|
20
|
-
|
21
4
|
before do
|
22
|
-
@
|
5
|
+
@user = FactoryBot.build(:user)
|
23
6
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
7
|
+
context 'ユーザー新規登録ができるとき' do
|
28
|
-
it
|
8
|
+
it '正しい情報を入力すればユーザー新規登録ができてトップページに移動する' do
|
9
|
+
# トップページに移動する
|
29
|
-
|
10
|
+
visit root_path
|
11
|
+
# トップページにサインアップページへ遷移するボタンがあることを確認する
|
12
|
+
expect(page).to have_content('Sign Up')
|
13
|
+
# 新規登録ページへ移動する
|
14
|
+
visit new_user_registration_path
|
15
|
+
# ユーザー情報を入力する
|
16
|
+
fill_in 'Nickname', with: @user.nickname
|
17
|
+
fill_in 'Email', with: @user.email
|
18
|
+
fill_in 'Password', with: @user.password
|
19
|
+
fill_in 'Password confirmation', with: @user.password_confirmation
|
20
|
+
# サインアップボタンを押すとユーザーモデルのカウントが1上がることを確認する
|
21
|
+
expect{
|
22
|
+
find('input[name="commit"]').click
|
23
|
+
}.to change { User.count }.by(1)
|
24
|
+
# トップページへ遷移する
|
30
|
-
expect(
|
25
|
+
expect(current_path).to eq root_path
|
26
|
+
# カーソルを合わせるとログアウトボタンが表示されることを確認する
|
27
|
+
expect(
|
28
|
+
find(".user_nav").fins("span").hover
|
29
|
+
).to have_content('Log Out')
|
30
|
+
# サインアップページへ遷移するボタンや、ログインページへ遷移するボタンが表示されていないことを確認する
|
31
|
+
expect(page).to have_no_content('Sign Up')
|
32
|
+
expect(page).to have_no_content('Log In')
|
31
33
|
end
|
34
|
+
end
|
35
|
+
context 'ユーザー新規登録ができないとき' do
|
32
|
-
it
|
36
|
+
it '誤った情報ではユーザー新規登録ができずに新規登録ページへ戻ってくる' do
|
33
|
-
|
37
|
+
# トップページに移動する
|
38
|
+
# トップページにサインアップページへ遷移するボタンがあることを確認する
|
39
|
+
# 新規登録ページへ移動する
|
40
|
+
# ユーザー情報を入力する
|
34
|
-
|
41
|
+
# サインアップボタンを押してもユーザーモデルのカウントは上がらないことを確認する
|
42
|
+
# 新規登録ページへ戻されることを確認する
|
35
43
|
end
|
36
|
-
it "indexアクションにリクエストするとレスポンスに登録済みのカロリーが存在する" do
|
37
|
-
get root_path
|
38
|
-
expect(response.body).to include @nutrition.calorie
|
39
|
-
end
|
40
|
-
it "indexアクションにリクエストするとレスポンスに登録済みのタンパク質が存在する" do
|
41
|
-
get root_path
|
42
|
-
expect(response.body).to include @nutrition.protein
|
43
|
-
end
|
44
|
-
|
45
|
-
it "indexアクションにリクエストするとレスポンスに投稿検索フォームが存在する" do
|
46
|
-
end
|
47
44
|
end
|
48
|
-
end
|
45
|
+
end
|
49
|
-
|
50
|
-
コード
|
51
|
-
```
|
52
|
-
|
53
|
-
float型であることに文句を言われているのでFactoriesが怪しいなと思い、色々型を変えてみたのですが、解決できませんでした。
|
54
|
-
【factories/nutritions.rb】
|
55
|
-
```FactoryBot.define do
|
56
|
-
factory :nutrition do
|
57
|
-
|
58
|
-
ingredient {Faker::Lorem.sentence}
|
59
|
-
calorie {"300"}
|
60
|
-
protein {"300"}
|
61
|
-
lipid {"300"}
|
62
|
-
carbohydrate {"300"}
|
63
|
-
potassium {"300"}
|
64
|
-
calcium {"300"}
|
65
|
-
iron {"300"}
|
66
|
-
vitamin_a {"300"}
|
67
|
-
vitamin_b1 {"300"}
|
68
|
-
vitamin_b2 {"300"}
|
69
|
-
vitamin_c {"300"}
|
70
|
-
dietary_fiber {"300"}
|
71
|
-
association :user
|
72
|
-
end
|
73
|
-
end
|
74
|
-
コード
|
75
|
-
```
|
76
|
-
|
77
|
-
対象のindex.html.erbは以下です。
|
78
|
-
|
79
|
-
```<div class="index-contents">
|
80
|
-
<div class="index-image">
|
81
|
-
<%= image_tag ('/index3.jpg') %>
|
82
|
-
</div>
|
83
|
-
</div>
|
84
|
-
<div class="index-text">
|
85
|
-
Beriiesは食材の各栄養素を管理できるアプリです。<br>
|
86
|
-
食材一覧からお好みの食材の栄養素をチェックしてみましょう。
|
87
|
-
</div>
|
88
|
-
|
89
|
-
|
90
|
-
<%= form_with(url: search_nutritions_path, local: true, method: :get, class: "search-form") do |form| %>
|
91
|
-
<%= form.text_field :keyword, placeholder: "さっそく食材を検索しよう!", class: "search-input" %>
|
92
|
-
<%= form.submit "Search", class: "search-btn" %>
|
93
|
-
<% if user_signed_in? %>
|
94
|
-
<div>検索しても無ければここから食材を登録しよう!</div>
|
95
|
-
<div class="index-new">
|
96
|
-
<%= link_to "食品登録", new_nutrition_path, class: "new-btn" %>
|
97
|
-
</div>
|
98
|
-
<% else %>
|
99
|
-
<div class="index-new">
|
100
|
-
<p class="letter-spacing"> 登録がなかったら新規登録してみましょう!<br>初めての方はここからかんたんログイン!</p>
|
101
|
-
<%= link_to 'かんたんログイン', users_guest_sign_in_path, method: :post, class: "new-btn"%>
|
102
|
-
<p class="login-select"> 登録済みの方 新規会員登録</p>
|
103
|
-
<%= link_to "Log In", new_user_session_path, class: "new-btn2" %>
|
104
|
-
<%= link_to "Sign Up", new_user_registration_path, class: "new-btn2" %>
|
105
|
-
</div>
|
106
|
-
<% end %>
|
107
|
-
|
108
|
-
|
109
|
-
<% end %>
|
110
|
-
|
111
|
-
<script src="delete.js"></script>
|
112
|
-
|
113
|
-
<div class='main-contents'>
|
114
|
-
|
115
|
-
<h1 class="contents-title">食品一覧<EM STYLE="font-size: large;">(100gあたり)</EM></h1>
|
116
|
-
<table border="5" width="80%" cellpadding="20" bordercolor="#882d91" class="contents-column" align="left">
|
117
|
-
<tr class="column-name">
|
118
|
-
<th width="30%" height="50">食品名</th>
|
119
|
-
<th class="column-color1" width="15%">エネルギー(kcal)</th>
|
120
|
-
<th class="column-color1" width="15%">タンパク質(g)</th>
|
121
|
-
<th class="column-color1" width="15%">脂質(g)</th>
|
122
|
-
<th class="column-color1" width="15%">炭水化物(g)</th>
|
123
|
-
<th class="column-color2" width="15%">カリウム(mg)</th>
|
124
|
-
<th class="column-color2" width="15%">カルシウム(mg)</th>
|
125
|
-
<th class="column-color2" width="15%">鉄(mg)</th>
|
126
|
-
<th class="column-color3" width="15%">ビタミンA(mg)</th>
|
127
|
-
<th class="column-color3" width="15%">ビタミンB1(mg)</th>
|
128
|
-
<th class="column-color3" width="15%">ビタミンB2(mg)</th>
|
129
|
-
<th class="column-color3" width="15%">ビタミンC(mg)</th>
|
130
|
-
<th width="15%">食物繊維(g)</th>
|
131
|
-
<%#<th width="15%">登録ユーザー</th>%>
|
132
|
-
</tr>
|
133
|
-
|
134
|
-
<% @nutritions.each do |nutrition| %>
|
135
|
-
<tr height="60" class="content-post">
|
136
|
-
<%# サインインしているかつ、お気に入り登録している食品は★がつき、カラーは赤になる%>
|
137
|
-
<% if user_signed_in? %>
|
138
|
-
<% if current_user.already_favorited?(nutrition, current_user) %>
|
139
|
-
<td class="ingredient-column fav-color" id="ingredient-column">★<%= nutrition.ingredient %>
|
140
|
-
<% else %>
|
141
|
-
<td class="ingredient-column" id="ingredient-column"><%= nutrition.ingredient %>
|
142
|
-
<% end %>
|
143
|
-
<% else %>
|
144
|
-
<td class="ingredient-column" id="ingredient-column"><%= nutrition.ingredient %>
|
145
|
-
<% end %>
|
146
|
-
<div class="more" id="more">
|
147
|
-
<ul class="more-list" id="more-list">
|
148
|
-
<li>
|
149
|
-
<% if user_signed_in? %>
|
150
|
-
<% if current_user.already_favorited?(nutrition, current_user) %>
|
151
|
-
<%# 解除の引数としてid:0を渡しているがこれが無いとidがないというエラーが起きてしまうので応急処置 %>
|
152
|
-
<%= link_to '解除', nutrition_favorites_path(user_id:current_user.id, nutrition_id:nutrition.id, id:0), :style=>"color:green;", method: :delete %>
|
153
|
-
<% else %>
|
154
|
-
<%= link_to '登録', user_favorites_path(user_id:current_user.id, nutrition_id:nutrition.id), :style=>"color:green;", method: :post %>
|
155
|
-
<% end %>
|
156
|
-
<% if current_user.id == nutrition.user.id %>
|
157
|
-
<%= link_to '編集', edit_nutrition_path(nutrition.id), :style=>"color:green;", method: :get %>
|
158
|
-
<%= link_to '削除', nutrition_path(nutrition.id), :style=>"color:green;", method: :delete, data: { confirm: '削除しますか?'} %>
|
159
|
-
<% end %>
|
160
|
-
<% end %>
|
161
|
-
</li>
|
162
|
-
</ul>
|
163
|
-
</div>
|
164
|
-
<td><%= number_with_precision(nutrition.calorie, precision: 1, strip_insignificant_zeros: true) %></td>
|
165
|
-
<td><%= number_with_precision(nutrition.protein, precision: 1, strip_insignificant_zeros: true) %></td>
|
166
|
-
<td><%= number_with_precision(nutrition.lipid, precision: 1, strip_insignificant_zeros: true) %></td>
|
167
|
-
<td><%= number_with_precision(nutrition.carbohydrate, precision: 1, strip_insignificant_zeros: true) %></td>
|
168
|
-
<td><%= number_with_precision(nutrition.potassium, precision: 1, strip_insignificant_zeros: true) %></td>
|
169
|
-
<td><%= number_with_precision(nutrition.calcium, precision: 1, strip_insignificant_zeros: true) %></td>
|
170
|
-
<td><%= number_with_precision(nutrition.iron, precision: 1, strip_insignificant_zeros: true) %></td>
|
171
|
-
<td><%= number_with_precision(nutrition.vitamin_a, precision: 1, strip_insignificant_zeros: true) %></td>
|
172
|
-
<td><%= number_with_precision(nutrition.vitamin_b1, precision: 1, strip_insignificant_zeros: true) %></td>
|
173
|
-
<td><%= number_with_precision(nutrition.vitamin_b2, precision: 1, strip_insignificant_zeros: true) %></td>
|
174
|
-
<td><%= number_with_precision(nutrition.vitamin_c, precision: 1, strip_insignificant_zeros: true) %></td>
|
175
|
-
<td><%= number_with_precision(nutrition.dietary_fiber, precision: 1, strip_insignificant_zeros: true) %></td>
|
176
|
-
</tr>
|
177
|
-
<% end %>
|
178
|
-
</table>
|
179
|
-
</div>
|
180
|
-
|
181
|
-
コード
|
182
|
-
```
|
183
|
-
|
184
|
-
また、マイグレーションファイルは以下になります。
|
185
|
-
```class ChangeDataAllNutrition < ActiveRecord::Migration[6.0]
|
186
|
-
def up
|
187
|
-
change_column :nutritions, :protein, :float
|
188
|
-
change_column :nutritions, :lipid, :float
|
189
|
-
change_column :nutritions, :carbohydrate, :float
|
190
|
-
change_column :nutritions, :potassium, :float
|
191
|
-
change_column :nutritions, :calcium, :float
|
192
|
-
change_column :nutritions, :iron, :float
|
193
|
-
change_column :nutritions, :vitamin_a, :float
|
194
|
-
change_column :nutritions, :vitamin_b1, :float
|
195
|
-
change_column :nutritions, :vitamin_b2, :float
|
196
|
-
change_column :nutritions, :vitamin_c, :float
|
197
|
-
change_column :nutritions, :dietary_fiber, :float
|
198
|
-
rename_column :nutritions, :dietary_fiber, :dietary_fiber
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
コード
|
203
|
-
```
|
204
|
-
|
205
|
-
お力添頂けますと幸いです。
|
1
tuki
title
CHANGED
File without changes
|
body
CHANGED
@@ -181,4 +181,25 @@
|
|
181
181
|
コード
|
182
182
|
```
|
183
183
|
|
184
|
+
また、マイグレーションファイルは以下になります。
|
185
|
+
```class ChangeDataAllNutrition < ActiveRecord::Migration[6.0]
|
186
|
+
def up
|
187
|
+
change_column :nutritions, :protein, :float
|
188
|
+
change_column :nutritions, :lipid, :float
|
189
|
+
change_column :nutritions, :carbohydrate, :float
|
190
|
+
change_column :nutritions, :potassium, :float
|
191
|
+
change_column :nutritions, :calcium, :float
|
192
|
+
change_column :nutritions, :iron, :float
|
193
|
+
change_column :nutritions, :vitamin_a, :float
|
194
|
+
change_column :nutritions, :vitamin_b1, :float
|
195
|
+
change_column :nutritions, :vitamin_b2, :float
|
196
|
+
change_column :nutritions, :vitamin_c, :float
|
197
|
+
change_column :nutritions, :dietary_fiber, :float
|
198
|
+
rename_column :nutritions, :dietary_fiber, :dietary_fiber
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
コード
|
203
|
+
```
|
204
|
+
|
184
205
|
お力添頂けますと幸いです。
|