質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2659閲覧

railsチュートリアル中に思いもよらないエラーが出ました

ninpig04

総合スコア33

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2017/12/25 19:36

Railsチュートリアル使って勉強しているのですが、テストでGREENになるはずのところがREDになって困っています。

エラー内容は以下の通りです

Started with run options --seed 22009 ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 0.7885890000034124] test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (0.79s) ArgumentError: ArgumentError: unknown keyword: id test/controllers/users_controller_test.rb:29:in `block in <class:UsersControllerTest>' ERROR["test_should_redirect_update_when_not_logged_in", UsersControllerTest, 0.7913689999986673] test_should_redirect_update_when_not_logged_in#UsersControllerTest (0.79s) ArgumentError: ArgumentError: unknown keywords: id, user test/controllers/users_controller_test.rb:22:in `block in <class:UsersControllerTest>' ERROR["test_should_redirect_edit_when_not_logged_in", UsersControllerTest, 0.7941770000034012] test_should_redirect_edit_when_not_logged_in#UsersControllerTest (0.79s) ArgumentError: ArgumentError: unknown keyword: id test/controllers/users_controller_test.rb:16:in `block in <class:UsersControllerTest>' ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 0.7966000000014901] test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (0.80s) ArgumentError: ArgumentError: unknown keywords: id, user test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'

テストの内容(test/controllers/users_controller_test.rb)箱のようになっています

require 'test_helper' class UsersControllerTest < ActionController::TestCase def setup @user = users(:michael) @other_user = users(:archer) end test "should get new" do get :new assert_response :success end test "should redirect edit when not logged in" do get :edit, id: @user assert_not flash.empty? assert_redirected_to login_url end test "should redirect update when not logged in" do patch :update, id: @user, user: { name: @user.name, email: @user.email } assert_not flash.empty? assert_redirected_to login_url end test "should redirect edit when logged in as wrong user" do log_in_as(@other_user) get :edit, id: @user assert flash.empty? assert_redirected_to root_url end test "should redirect update when logged in as wrong user" do log_in_as(@other_user) patch :update, id: @user, user: { name: @user.name, email: @user.email } assert flash.empty? assert_redirected_to root_url end end

app/controllers/users_controller.rbの内容はのこのようになっています

class UsersController < ApplicationController before_action :logged_in_user,only:[:edit,:update] before_action :correct_user, only: [:edit, :update] def show @user = User.find(params[:id]) end def new @user = User.new end def create @user = User.new(user_params) if @user.save log_in @user flash[:success] = "Welcome to the Sample App!" redirect_to @user else render 'new' end end def edit end def update if @user.update_attributes(user_params) flash[:success] = "Profile updated" redirect_to @user else render 'edit' end end private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end def logged_in_user unless logged_in? store_location flash[:danger] = "Please log in." redirect_to login_url end end def correct_user @user = User.find(params[:id]) redirect_to(root_url) unless @user == current_user end end

何がいけないのでしょうか?
チュートリアル通りにやっているので、もしかして何か仕様が変わっているのかも?と思っています
よろしくです

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

コードを書き間違えている可能性があるので,Rails チュートリアルのどの箇所かを書いていただけると探すのが楽ですね。

失敗しているテスト should redirect edit when not logged in を見比べると

rb

1 get :edit, id: @user

が Rails チュートリアルの リスト10.20 では

rb

1 get edit_user_path(@user)

となっているようですが。

投稿2017/12/25 21:12

scivola

総合スコア2108

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ninpig04

2017/12/26 08:33

修正しましたが、やはりこのようなメッセージが出ます 何が悪いのでしょうか? ERROR["test_should_redirect_update_when_not_logged_in", UsersControllerTest, 0.7742910000088159] test_should_redirect_update_when_not_logged_in#UsersControllerTest (0.77s) ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"/users/762146111", :controller=>"users", :user=>{:name=>"Michael Example", :email=>"michael@example.com"}} test/controllers/users_controller_test.rb:22:in `block in <class:UsersControllerTest>' ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 0.7881170000036946] test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (0.79s) ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"/users/762146111/edit", :controller=>"users"} test/controllers/users_controller_test.rb:30:in `block in <class:UsersControllerTest>' ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 0.7913680000056047] test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (0.79s) ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"/users/762146111", :controller=>"users", :user=>{:name=>"Michael Example", :email=>"michael@example.com"}} test/controllers/users_controller_test.rb:37:in `block in <class:UsersControllerTest>' ERROR["test_should_redirect_edit_when_not_logged_in", UsersControllerTest, 0.7962039999983972] test_should_redirect_edit_when_not_logged_in#UsersControllerTest (0.80s) ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"/users/762146111/edit", :controller=>"users"} test/controllers/users_controller_test.rb:16:in `block in <class:UsersControllerTest>'
scivola

2017/12/26 09:48

No route matches となっているので,config/routes.rb を見せてください。 あるいは,当初の問題は解決して別の問題に移っているのと思うので,別の質問を立ててください。
ninpig04

2017/12/26 09:54

ルートの中身はこのようになっています Rails.application.routes.draw do get 'sessions/new' get 'users/new' root 'static_pages#home' get '/help', to:'static_pages#help' get '/about', to:'static_pages#about' get '/contact', to:'static_pages#contact' get '/signup', to:'users#new' post '/signup', to: 'users#create' get '/login', to:'sessions#new' post '/login', to:'sessions#create' delete '/logout', to:'sessions#destroy' resources :users # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root 'static_pages#home' end これもrailsチュートリアルに従って記述したものです
scivola

2017/12/26 12:08 編集

routes.rb に get 'users/new' があるのは奇妙です。resources :users があるなら要らないはず。 ただ,あっても害は無いはずで,これが問題の原因ではないとは思いますが。
scivola

2017/12/26 12:18

"test_should_redirect_update_when_not_logged_in" のエラーメッセージのところで, :action=>"/users/762146111" となっているのが異常ですね。アクション名は edit になるはず。 テストコードが正しく get edit_user_path(@user) と書けているなら,@user の値がおかしいのかも? @user を表示してみてはいかがでしょう(byebug を知っているならそれで,知らないならプリントデバッグでもいいので→つまり,この直前に p @user と書く)。 それと test/fixtures/users.yml がチュートリアルどおりに正しく書けているかも確認してみましょう。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問