1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

改修後にrailsアプリで急にActionController::RoutingErrorが出るようになった時の対処法

Posted at

#はじめに
railsチュートリアルの10章10.2.2 正しいユーザーを要求するを学習中に
2日ほど大ハマリしたので、戒めとして残しておきます。

##動作環境

  • cloud9
  • ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
  • Rails 5.0.0.1

##結論
追加した処理コードの後ろに、全角スペースが入っていたので、除去した。

##問題の経緯
railsアプリケーションを改修後、testコードでエラーが出るようになった。
Routing周りは修正はしておらず、コードのタイプミスもない。

発生しているエラーは以下の通り。

yokoyan:~/workspace/sample_app (updating-users) $ rails test
Running via Spring preloader in process 2393
Started with run options --seed 27329

ERROR["test_login_with_valid_information_followed_by_logout", UsersLoginTest, 0.6586209130473435]
 test_login_with_valid_information_followed_by_logout#UsersLoginTest (0.66s)
ActionController::RoutingError:         ActionController::RoutingError: undefined local variable or method ` ' for UsersController:Class
            app/controllers/users_controller.rb:60:in `<class:UsersController>'
            app/controllers/users_controller.rb:1:in `<top (required)>'
            test/integration/users_login_test.rb:28:in `block in <class:UsersLoginTest>'

ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 0.9663001720327884]
 test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (0.97s)
NoMethodError:         NoMethodError: undefined method `correct_user' for #<UsersController:0x0000000534c678>
        Did you mean?  current_user
                       current_user?
            test/controllers/users_controller_test.rb:30:in `block in <class:UsersControllerTest>'

ERROR["test_successful_edit", UsersEditTest, 1.1715300020296127]
 test_successful_edit#UsersEditTest (1.17s)
NoMethodError:         NoMethodError: undefined method `correct_user' for #<UsersController:0x00000005545f60>
        Did you mean?  current_user
                       current_user?
            test/integration/users_edit_test.rb:26:in `block in <class:UsersEditTest>'

ERROR["test_unsuccessful_edit", UsersEditTest, 1.2108383220620453]
 test_unsuccessful_edit#UsersEditTest (1.21s)
NoMethodError:         NoMethodError: undefined method `correct_user' for #<UsersController:0x00000005aa5a00>
        Did you mean?  current_user
                       current_user?
            test/integration/users_edit_test.rb:14:in `block in <class:UsersEditTest>'

  34/34: [========================================================================================================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.22472s
34 tests, 70 assertions, 0 failures, 4 errors, 0 skips

##参考になった情報
7章ユーザ登録まで

このendの後に、全角スペースが入っていた事が原因でした、、

!!!!

##対策
書いたコードの全角スペースを一括置換。
今回の対象は、以下の2つ。

/sample_app/test/controllers/users_controller_test.rb
/sample_app/app/controllers/users_controller.rb

テキストエディタでやってもいいし、以下のようなサービスもあります。

空白行や行頭スペース・タブ、全角スペースの削除ツール

##再度テストコードを実施
無事にテストコードが実行できるようになりました。

yokoyan:~/workspace/sample_app (updating-users) $ rails test
Running via Spring preloader in process 2487
Started with run options --seed 34867

  34/34: [========================================================================================================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.29474s
34 tests, 91 assertions, 0 failures, 0 errors, 0 skip

#おわりに
全角スペースには、本当に気をつけましょう。
とほほ。。。

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?