回答編集履歴
1
自己解決したので解決方法を記入しました。
answer
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
|
1
|
+
エラー原因は、renderの使い方が間違っていたことでした。
|
2
2
|
|
3
|
-
以下、エラー原因について記載。
|
4
|
-
|
5
|
-
コントローラをprintデバッグしたところ、コントローラは1→2→3→6→7行目の順に処理されていることがわかりました。
|
3
|
+
コントローラをprintデバッグしたところ、コントローラは、意図したとおり1→2→3→6→7行目の順に処理されていることがわかりました。
|
6
4
|
また、@userの値をチェックしたところ、elsifの前後で値が変わっていることがわかりました。
|
7
5
|
さらに、elsifの後にputsを入れたところ、エラー内容が変わりました(下記参照)。
|
8
6
|
|
@@ -27,6 +25,34 @@
|
|
27
25
|
Response body:
|
28
26
|
test/integration/users_edit_test.rb:17:in `block in <class:UsersEditTest>'
|
29
27
|
```
|
28
|
+
したがって、8行目のrenderに問題があると判断してrenderの使い方を調べました。
|
29
|
+
(参考URL https://railsguides.jp/layouts_and_rendering.html)
|
30
|
+
すると、同じコントローラ内の別のアクションのテンプレートを表示したいときには、render :actionまたはrender "action"と書けばよいことがわかりました。
|
30
31
|
|
32
|
+
修正したところ、テストのエラー箇所がupdateから、その次の行のassert_redirected_to @userに変わりました。
|
33
|
+
コントローラでリダイレクトをしていないので、assert_redirected_toをassert_templateに修正し、リダイレクト先がユーザーページでなく編集ページなので@userから:editに変更したところ、エラーが出なくなり、Web上でも問題なく動作するようになりました。
|
34
|
+
|
35
|
+
修正後のコントローラ
|
36
|
+
```Ruby
|
37
|
+
def update
|
38
|
+
@user = User.find(params[:user_id])
|
39
|
+
if @user.update_attributes(user_params)
|
40
|
+
flash[:success] = "登録情報を更新しました"
|
41
|
+
redirect_to @user
|
42
|
+
elsif
|
43
|
+
render :edit
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
修正後のテスト
|
48
|
+
```Ruby
|
31
|
-
|
49
|
+
test "unsuccessful setting edit" do
|
50
|
+
log_in_as(@user)
|
51
|
+
get edit_user_setting_path(@user)
|
52
|
+
patch user_setting_path(@user), params: { user: { name: "",
|
53
|
+
email: "foo@invalid",
|
54
|
+
password: "foo",
|
55
|
+
password_confirmation: "bar" } }
|
32
|
-
|
56
|
+
assert_template :edit
|
57
|
+
end
|
58
|
+
```
|