LoginSignup
Tsuku
@Tsuku

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Herokuアプリが急に機能しなくなった

解決したいこと

HerokuにRailsアプリをデプロイし、2~3日前までは正常に動作していたのだが、急にログイン機能やその他の機能が正常に動作しなくなった。ブラウザにToo many redirectsのエラーが表示され、アプリのトップページから先に進めなくなってしまった。「では、ローカル環境では?」と思い、動かしてみるもローカルですら動かなくなっておりわけがわからない。

追記:
ついに、トップページすら開かなくなってしまいました。表示は、同じくtoo many redirectsとのことです。

どなたか、知恵を貸していただけないでしょうか。よろしくお願いいたします。

該当のアプリ
https://tsuku-heroku-first-app-0001.herokuapp.com/
Screen Shot 2021-11-16 at 19.56.50.png

該当するソースコード

#users_controller.rb
#見ていただきたいのは、loginメソッドである。
#管理者も一般ユーザもUserテーブルに保存されており、
#administratorというカラムに"1"が入っているのを管理者としている。
#一般ユーザのそれには"0"が入っている。
#users_controllerのloginメソッドは、この1か0かを見てリダイレクト先を変えている。

    class UsersController < ApplicationController
    before_action :youNeedToLogIn, {except: [:youAreNotCorrectUser, :login_form, :login, :new, :create]}
    before_action :forbidLoginUser, {only: [:new, :login_form]}
    before_action :youAreNotCorrectUser, {except: [:login_form, :login, :logout, :new, :create]}

    #あるログインユーザーが他ユーザのidをURLに含めてリクエストすることで他ユーザのページたちを閲覧するのをブロックする
    def youAreNotCorrectUser
        if @current_user.id != params[:id].to_i
            flash[:notice] = "他ユーザーの領域に入ることはできません"
            redirect_to("/users/#{@current_user.id}/home")
        end
    end

    #ログイン画面へ移動
    def login_form
    end

    #ログインのためDBとやりとり
    def login
        @user = User.find_by(name: params[:name],
                         password: params[:password])
        if @user
            flash[:notice] = "ログインしました"
            session[:user_id] = @user.id
            if @user.administrator == true
                redirect_to("/administrator/home")
            else 
                redirect_to("/users/#{session[:user_id]}/home")
            end
        else
            flash[:notice] = "アカウントが存在しないか、ログイン情報が間違っています"
            @name = params[:name]
            @password = params[:password]
            render("users/login_form")
        end
    end

    def logout
        flash[:notice] = "ログアウトしました"
        session[:user_id] = nil
        redirect_to("/")
    end

    def user_home
        @user = User.find_by(id: params[:id])
        if @user.administrator == true
            redirect_to("/administrator/home")
        else
            redirect_to("/users/#{@user.id}/home")
        end
    end

    def user_profile
        @user = User.find_by(id: @current_user.id)
    end

    def userinfo_edit
        @user = User.find_by(id: params[:id])
        @user.name = params[:name]
        @user.password = params[:password]
        if @user.save
            flash[:notice] = "ユーザー情報が変更されました"
            redirect_to("/users/#{@current_user.id}/home")
        else
            flash[:notice] = "ユーザー情報が変更できません"
            render("users/user_profile")
        end
    end

    def delete_account
        @user = User.find_by(id: params[:id])
        if @user.administrator == true
            flash[:notice] = "管理人アカウントは削除できません"
            render("users/user_profile")
        else
            @user.destroy
            flash[:notice] = "#{@user.name}のアカウントが削除されました"
            redirect_to("/")
        end

    end

    #新規ユーザー登録画面へ移動
    def new
    end

    # 新規ユーザーをDBに追加
    def create
        @user = User.new(name: params[:name],
                         password: params[:password])
        if @user.save
            flash[:notice] = "アカウントを作成しました"
            session[:user_id] = @user.id
            redirect_to("/users/#{session[:user_id]}/home")
        else
            flash[:notice] = "保存できていません"
            @name = params[:name]
            @password = params[:password]
            render("users/new")
        end
    end

    #問題一覧を表示する
    def index
        @original_qas = Qn.where(originality: "yes")
        @yourqas = Qn.where(user_id: @current_user.id)
        if @yourqas.empty?
            @noqas_message = "あなたが作った問題はありません。何か作ってみませんか?"
        end
    end

    #クイズ追加画面へ移動
    def add_yourqn
        @yourqa = Qn.new
    end

    #収録問題出題画面へ移動
    def give_qas
        qasFromTable = Qn.where(originality: "yes")
        shuffledQas = qasFromTable.shuffle
        gon.qns = shuffledQas.pluck(:question)
        gon.anss = shuffledQas.pluck(:answer)
    end

    #ユーザー作成の問題出題画面へ移動
    def give_userqas
        qasFromTable = Qn.where(user_id: @current_user.id)
        shuffledQas = qasFromTable.shuffle
        gon.qns = shuffledQas.pluck(:question)
        gon.anss = shuffledQas.pluck(:answer)
    end
end

#administrator_controller.rb
#管理者も一般ユーザもUserテーブルに保存されており、
#administratorというカラムに"1"が入っているのを管理者としている。
#一般ユーザのそれには"0"が入っている。
#users_controllerのloginメソッドは、この1か0かを見てリダイレクト先を変えている。

class AdministratorController < ApplicationController
    before_action :youNeedToLogIn

    def admin_home
    end

    def qanda_index
        @qanda = Qn.where(originality: "yes")
    end

    def add_qas
        @qanda = Qn.new
    end

    def create
        @qanda = Qn.new(question: params[:question],
                        answer: params[:answer],
                        originality: "yes")
        if @qanda.save
            redirect_to("/administrator/index")
        else
            @question = params[:question]
            @answer = params[:answer]
            render("administrator/add_qas")
        end
    end

    def qanda_editform
        @qanda = Qn.find_by(id: params[:id])
    end

    def qanda_edit
        @qanda = Qn.find_by(id: params[:id])
        @qanda.question = params[:question]
        @qanda.answer = params[:answer]
        if @qanda.save
            flash[:notice] = "管理者としてクイズを編集しました"
            redirect_to("/administrator/home")
        else
            @qanda = Qn.find_by(id: params[:id])
            render("administrator/qanda_editform")
        end
    end
end
#routes.rb

Rails.application.routes.draw do

  get "administrator/home" => "administrator#admin_home"
  get "administrator/index" => "administrator#qanda_index"
  get "administrator/addqas" => "administrator#add_qas"
  get "administrator/:id/qandaeditform" => "administrator#qanda_editform"
  post "administrator/:id/qandaedit" => "administrator#qanda_edit"
  post "administrator/create" => "administrator#create"

  get "loginform" => "users#login_form"
  get "signup" => "users#new"
  post "create" => "users#create"
  post "login" => "users#login"
  post "logout" => "users#logout"

  get "users/:id/home" => "users#user_home"
  get "users/:id/profile" => "users#user_profile"
  get "users/:id/index" => "users#index"
  get "users/:id/addyourqn" => "users#add_yourqn"
  get "users/:id/giveqas" => "users#give_qas"
  get "users/:id/giveuserqas" => "users#give_userqas"
  post "users/:id/userinfoedit" => "users#userinfo_edit"
  post "users/:id/deleteaccount" => "users#delete_account"

  get "qns/:id/editform" => "qns#edit_form"
  get "qns/:id/show" => "qns#show_userqas"
  post "qns/:id/edit" => "qns#edit"
  post "qns/:id/destroy" => "qns#destroy"
  post "qns/create" => "qns#create"

  get "/" => "home#top"
  get "about" => "home#about"

end
↓heroku logs --tailを実行した結果

2021-11-16T10:26:48.779811+00:00 app[web.1]: I, [2021-11-16T10:26:48.779792 #4]  INFO -- : [4a83dc4a-e6c4-4a35-9d0d-1a5070e57ab5]   Parameters: {"id"=>"3"}
2021-11-16T10:26:48.785373+00:00 app[web.1]: I, [2021-11-16T10:26:48.785264 #4]  INFO -- : [4a83dc4a-e6c4-4a35-9d0d-1a5070e57ab5] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:48.785491+00:00 app[web.1]: I, [2021-11-16T10:26:48.785464 #4]  INFO -- : [4a83dc4a-e6c4-4a35-9d0d-1a5070e57ab5] Completed 302 Found in 6ms (ActiveRecord: 2.5ms | Allocations: 524)
2021-11-16T10:26:48.786326+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=4a83dc4a-e6c4-4a35-9d0d-1a5070e57ab5 fwd="106.180.6.156" dyno=web.1 connect=0ms service=8ms status=302 bytes=1212 protocol=https
2021-11-16T10:26:49.019429+00:00 app[web.1]: I, [2021-11-16T10:26:49.019358 #4]  INFO -- : [3b925313-24e6-4036-a263-00cc38913e2d] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:49 +0000
2021-11-16T10:26:49.020062+00:00 app[web.1]: I, [2021-11-16T10:26:49.020006 #4]  INFO -- : [3b925313-24e6-4036-a263-00cc38913e2d] Processing by UsersController#user_home as HTML
2021-11-16T10:26:49.020085+00:00 app[web.1]: I, [2021-11-16T10:26:49.020060 #4]  INFO -- : [3b925313-24e6-4036-a263-00cc38913e2d]   Parameters: {"id"=>"3"}
2021-11-16T10:26:49.025270+00:00 app[web.1]: I, [2021-11-16T10:26:49.025219 #4]  INFO -- : [3b925313-24e6-4036-a263-00cc38913e2d] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:49.025384+00:00 app[web.1]: I, [2021-11-16T10:26:49.025352 #4]  INFO -- : [3b925313-24e6-4036-a263-00cc38913e2d] Completed 302 Found in 5ms (ActiveRecord: 2.5ms | Allocations: 508)
2021-11-16T10:26:49.026246+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=3b925313-24e6-4036-a263-00cc38913e2d fwd="106.180.6.156" dyno=web.1 connect=0ms service=7ms status=302 bytes=1218 protocol=https
2021-11-16T10:26:49.322721+00:00 app[web.1]: I, [2021-11-16T10:26:49.322653 #4]  INFO -- : [b8effd6f-541d-4cdb-a89d-35c5c2b79692] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:49 +0000
2021-11-16T10:26:49.323326+00:00 app[web.1]: I, [2021-11-16T10:26:49.323281 #4]  INFO -- : [b8effd6f-541d-4cdb-a89d-35c5c2b79692] Processing by UsersController#user_home as HTML
2021-11-16T10:26:49.323347+00:00 app[web.1]: I, [2021-11-16T10:26:49.323328 #4]  INFO -- : [b8effd6f-541d-4cdb-a89d-35c5c2b79692]   Parameters: {"id"=>"3"}
2021-11-16T10:26:49.328859+00:00 app[web.1]: I, [2021-11-16T10:26:49.328793 #4]  INFO -- : [b8effd6f-541d-4cdb-a89d-35c5c2b79692] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:49.329014+00:00 app[web.1]: I, [2021-11-16T10:26:49.328975 #4]  INFO -- : [b8effd6f-541d-4cdb-a89d-35c5c2b79692] Completed 302 Found in 6ms (ActiveRecord: 2.5ms | Allocations: 514)
2021-11-16T10:26:49.329810+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=b8effd6f-541d-4cdb-a89d-35c5c2b79692 fwd="106.180.6.156" dyno=web.1 connect=0ms service=8ms status=302 bytes=1214 protocol=https
2021-11-16T10:26:49.639629+00:00 app[web.1]: I, [2021-11-16T10:26:49.639537 #4]  INFO -- : [e0bd4e35-4703-4933-b75e-c3ddff93b236] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:49 +0000
2021-11-16T10:26:49.640341+00:00 app[web.1]: I, [2021-11-16T10:26:49.640299 #4]  INFO -- : [e0bd4e35-4703-4933-b75e-c3ddff93b236] Processing by UsersController#user_home as HTML
2021-11-16T10:26:49.640368+00:00 app[web.1]: I, [2021-11-16T10:26:49.640349 #4]  INFO -- : [e0bd4e35-4703-4933-b75e-c3ddff93b236]   Parameters: {"id"=>"3"}
2021-11-16T10:26:49.645801+00:00 app[web.1]: I, [2021-11-16T10:26:49.645742 #4]  INFO -- : [e0bd4e35-4703-4933-b75e-c3ddff93b236] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:49.645966+00:00 app[web.1]: I, [2021-11-16T10:26:49.645925 #4]  INFO -- : [e0bd4e35-4703-4933-b75e-c3ddff93b236] Completed 302 Found in 6ms (ActiveRecord: 2.5ms | Allocations: 510)
2021-11-16T10:26:49.646745+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=e0bd4e35-4703-4933-b75e-c3ddff93b236 fwd="106.180.6.156" dyno=web.1 connect=0ms service=8ms status=302 bytes=1220 protocol=https
2021-11-16T10:26:49.880193+00:00 app[web.1]: I, [2021-11-16T10:26:49.880102 #4]  INFO -- : [2daef266-d43a-4410-81c7-aa8c50ca664a] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:49 +0000
2021-11-16T10:26:49.880890+00:00 app[web.1]: I, [2021-11-16T10:26:49.880839 #4]  INFO -- : [2daef266-d43a-4410-81c7-aa8c50ca664a] Processing by UsersController#user_home as HTML
2021-11-16T10:26:49.880906+00:00 app[web.1]: I, [2021-11-16T10:26:49.880886 #4]  INFO -- : [2daef266-d43a-4410-81c7-aa8c50ca664a]   Parameters: {"id"=>"3"}
2021-11-16T10:26:49.886336+00:00 app[web.1]: I, [2021-11-16T10:26:49.886281 #4]  INFO -- : [2daef266-d43a-4410-81c7-aa8c50ca664a] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:49.886447+00:00 app[web.1]: I, [2021-11-16T10:26:49.886413 #4]  INFO -- : [2daef266-d43a-4410-81c7-aa8c50ca664a] Completed 302 Found in 5ms (ActiveRecord: 2.7ms | Allocations: 516)
2021-11-16T10:26:49.887617+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=2daef266-d43a-4410-81c7-aa8c50ca664a fwd="106.180.6.156" dyno=web.1 connect=0ms service=8ms status=302 bytes=1214 protocol=https
2021-11-16T10:26:50.140249+00:00 app[web.1]: I, [2021-11-16T10:26:50.140188 #4]  INFO -- : [a82e010e-808c-46b5-a391-e22ac8ca7684] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:50 +0000
2021-11-16T10:26:50.140833+00:00 app[web.1]: I, [2021-11-16T10:26:50.140795 #4]  INFO -- : [a82e010e-808c-46b5-a391-e22ac8ca7684] Processing by UsersController#user_home as HTML
2021-11-16T10:26:50.140870+00:00 app[web.1]: I, [2021-11-16T10:26:50.140842 #4]  INFO -- : [a82e010e-808c-46b5-a391-e22ac8ca7684]   Parameters: {"id"=>"3"}
2021-11-16T10:26:50.145985+00:00 app[web.1]: I, [2021-11-16T10:26:50.145937 #4]  INFO -- : [a82e010e-808c-46b5-a391-e22ac8ca7684] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:50.146092+00:00 app[web.1]: I, [2021-11-16T10:26:50.146069 #4]  INFO -- : [a82e010e-808c-46b5-a391-e22ac8ca7684] Completed 302 Found in 5ms (ActiveRecord: 2.5ms | Allocations: 510)
2021-11-16T10:26:50.146903+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=a82e010e-808c-46b5-a391-e22ac8ca7684 fwd="106.180.6.156" dyno=web.1 connect=0ms service=7ms status=302 bytes=1218 protocol=https
2021-11-16T10:26:50.380673+00:00 app[web.1]: I, [2021-11-16T10:26:50.380597 #4]  INFO -- : [04b6c81c-5f95-4e7a-ac49-970125a357cd] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:50 +0000
2021-11-16T10:26:50.381247+00:00 app[web.1]: I, [2021-11-16T10:26:50.381204 #4]  INFO -- : [04b6c81c-5f95-4e7a-ac49-970125a357cd] Processing by UsersController#user_home as HTML
2021-11-16T10:26:50.381271+00:00 app[web.1]: I, [2021-11-16T10:26:50.381251 #4]  INFO -- : [04b6c81c-5f95-4e7a-ac49-970125a357cd]   Parameters: {"id"=>"3"}
2021-11-16T10:26:50.386607+00:00 app[web.1]: I, [2021-11-16T10:26:50.386548 #4]  INFO -- : [04b6c81c-5f95-4e7a-ac49-970125a357cd] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:50.386707+00:00 app[web.1]: I, [2021-11-16T10:26:50.386684 #4]  INFO -- : [04b6c81c-5f95-4e7a-ac49-970125a357cd] Completed 302 Found in 5ms (ActiveRecord: 2.5ms | Allocations: 514)
2021-11-16T10:26:50.387467+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=04b6c81c-5f95-4e7a-ac49-970125a357cd fwd="106.180.6.156" dyno=web.1 connect=0ms service=7ms status=302 bytes=1210 protocol=https
2021-11-16T10:26:50.619684+00:00 app[web.1]: I, [2021-11-16T10:26:50.619608 #4]  INFO -- : [d6ee82e3-01b1-4bec-90c2-0f70e99b588f] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:50 +0000
2021-11-16T10:26:50.620483+00:00 app[web.1]: I, [2021-11-16T10:26:50.620426 #4]  INFO -- : [d6ee82e3-01b1-4bec-90c2-0f70e99b588f] Processing by UsersController#user_home as HTML
2021-11-16T10:26:50.620517+00:00 app[web.1]: I, [2021-11-16T10:26:50.620492 #4]  INFO -- : [d6ee82e3-01b1-4bec-90c2-0f70e99b588f]   Parameters: {"id"=>"3"}
2021-11-16T10:26:50.626505+00:00 app[web.1]: I, [2021-11-16T10:26:50.626441 #4]  INFO -- : [d6ee82e3-01b1-4bec-90c2-0f70e99b588f] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:50.626670+00:00 app[web.1]: I, [2021-11-16T10:26:50.626633 #4]  INFO -- : [d6ee82e3-01b1-4bec-90c2-0f70e99b588f] Completed 302 Found in 6ms (ActiveRecord: 2.6ms | Allocations: 506)
2021-11-16T10:26:50.627679+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=d6ee82e3-01b1-4bec-90c2-0f70e99b588f fwd="106.180.6.156" dyno=web.1 connect=0ms service=9ms status=302 bytes=1208 protocol=https
2021-11-16T10:26:50.859188+00:00 app[web.1]: I, [2021-11-16T10:26:50.859126 #4]  INFO -- : [e9353f04-dd91-467e-b916-67d8a5bb18b7] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:50 +0000
2021-11-16T10:26:50.859796+00:00 app[web.1]: I, [2021-11-16T10:26:50.859752 #4]  INFO -- : [e9353f04-dd91-467e-b916-67d8a5bb18b7] Processing by UsersController#user_home as HTML
2021-11-16T10:26:50.859819+00:00 app[web.1]: I, [2021-11-16T10:26:50.859800 #4]  INFO -- : [e9353f04-dd91-467e-b916-67d8a5bb18b7]   Parameters: {"id"=>"3"}
2021-11-16T10:26:50.864924+00:00 app[web.1]: I, [2021-11-16T10:26:50.864870 #4]  INFO -- : [e9353f04-dd91-467e-b916-67d8a5bb18b7] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:50.865024+00:00 app[web.1]: I, [2021-11-16T10:26:50.864994 #4]  INFO -- : [e9353f04-dd91-467e-b916-67d8a5bb18b7] Completed 302 Found in 5ms (ActiveRecord: 2.5ms | Allocations: 504)
2021-11-16T10:26:50.865880+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=e9353f04-dd91-467e-b916-67d8a5bb18b7 fwd="106.180.6.156" dyno=web.1 connect=0ms service=7ms status=302 bytes=1208 protocol=https
2021-11-16T10:26:51.079085+00:00 app[web.1]: I, [2021-11-16T10:26:51.079022 #4]  INFO -- : [a7c31b4a-53ad-42c4-92ac-8756d2034e97] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:51 +0000
2021-11-16T10:26:51.079710+00:00 app[web.1]: I, [2021-11-16T10:26:51.079668 #4]  INFO -- : [a7c31b4a-53ad-42c4-92ac-8756d2034e97] Processing by UsersController#user_home as HTML
2021-11-16T10:26:51.079738+00:00 app[web.1]: I, [2021-11-16T10:26:51.079718 #4]  INFO -- : [a7c31b4a-53ad-42c4-92ac-8756d2034e97]   Parameters: {"id"=>"3"}
2021-11-16T10:26:51.085304+00:00 app[web.1]: I, [2021-11-16T10:26:51.085232 #4]  INFO -- : [a7c31b4a-53ad-42c4-92ac-8756d2034e97] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:51.085458+00:00 app[web.1]: I, [2021-11-16T10:26:51.085417 #4]  INFO -- : [a7c31b4a-53ad-42c4-92ac-8756d2034e97] Completed 302 Found in 6ms (ActiveRecord: 2.6ms | Allocations: 504)
2021-11-16T10:26:51.086284+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=a7c31b4a-53ad-42c4-92ac-8756d2034e97 fwd="106.180.6.156" dyno=web.1 connect=0ms service=8ms status=302 bytes=1220 protocol=https
2021-11-16T10:26:51.330824+00:00 app[web.1]: I, [2021-11-16T10:26:51.330755 #4]  INFO -- : [0174ef4b-c6d1-423b-8d7d-51e91aa5ee08] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:51 +0000
2021-11-16T10:26:51.331503+00:00 app[web.1]: I, [2021-11-16T10:26:51.331457 #4]  INFO -- : [0174ef4b-c6d1-423b-8d7d-51e91aa5ee08] Processing by UsersController#user_home as HTML
2021-11-16T10:26:51.331536+00:00 app[web.1]: I, [2021-11-16T10:26:51.331510 #4]  INFO -- : [0174ef4b-c6d1-423b-8d7d-51e91aa5ee08]   Parameters: {"id"=>"3"}
2021-11-16T10:26:51.338276+00:00 app[web.1]: I, [2021-11-16T10:26:51.338218 #4]  INFO -- : [0174ef4b-c6d1-423b-8d7d-51e91aa5ee08] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:51.338384+00:00 app[web.1]: I, [2021-11-16T10:26:51.338360 #4]  INFO -- : [0174ef4b-c6d1-423b-8d7d-51e91aa5ee08] Completed 302 Found in 7ms (ActiveRecord: 2.5ms | Allocations: 516)
2021-11-16T10:26:51.339301+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=0174ef4b-c6d1-423b-8d7d-51e91aa5ee08 fwd="106.180.6.156" dyno=web.1 connect=0ms service=21ms status=302 bytes=1226 protocol=https
2021-11-16T10:26:51.560973+00:00 app[web.1]: I, [2021-11-16T10:26:51.560899 #4]  INFO -- : [28957511-8d0f-4dff-899d-c21ea7cf4448] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:51 +0000
2021-11-16T10:26:51.561686+00:00 app[web.1]: I, [2021-11-16T10:26:51.561631 #4]  INFO -- : [28957511-8d0f-4dff-899d-c21ea7cf4448] Processing by UsersController#user_home as HTML
2021-11-16T10:26:51.561724+00:00 app[web.1]: I, [2021-11-16T10:26:51.561692 #4]  INFO -- : [28957511-8d0f-4dff-899d-c21ea7cf4448]   Parameters: {"id"=>"3"}
2021-11-16T10:26:51.567516+00:00 app[web.1]: I, [2021-11-16T10:26:51.567448 #4]  INFO -- : [28957511-8d0f-4dff-899d-c21ea7cf4448] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:51.567728+00:00 app[web.1]: I, [2021-11-16T10:26:51.567681 #4]  INFO -- : [28957511-8d0f-4dff-899d-c21ea7cf4448] Completed 302 Found in 6ms (ActiveRecord: 2.6ms | Allocations: 522)
2021-11-16T10:26:51.568765+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=28957511-8d0f-4dff-899d-c21ea7cf4448 fwd="106.180.6.156" dyno=web.1 connect=0ms service=10ms status=302 bytes=1238 protocol=https
2021-11-16T10:26:51.801293+00:00 app[web.1]: I, [2021-11-16T10:26:51.801232 #4]  INFO -- : [007faeb4-184e-4cc0-8598-3ca1ef4f105d] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:51 +0000
2021-11-16T10:26:51.801910+00:00 app[web.1]: I, [2021-11-16T10:26:51.801869 #4]  INFO -- : [007faeb4-184e-4cc0-8598-3ca1ef4f105d] Processing by UsersController#user_home as HTML
2021-11-16T10:26:51.801938+00:00 app[web.1]: I, [2021-11-16T10:26:51.801919 #4]  INFO -- : [007faeb4-184e-4cc0-8598-3ca1ef4f105d]   Parameters: {"id"=>"3"}
2021-11-16T10:26:51.806993+00:00 app[web.1]: I, [2021-11-16T10:26:51.806943 #4]  INFO -- : [007faeb4-184e-4cc0-8598-3ca1ef4f105d] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:51.807098+00:00 app[web.1]: I, [2021-11-16T10:26:51.807074 #4]  INFO -- : [007faeb4-184e-4cc0-8598-3ca1ef4f105d] Completed 302 Found in 5ms (ActiveRecord: 2.4ms | Allocations: 534)
2021-11-16T10:26:51.808636+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=007faeb4-184e-4cc0-8598-3ca1ef4f105d fwd="106.180.6.156" dyno=web.1 connect=0ms service=8ms status=302 bytes=1212 protocol=https
2021-11-16T10:26:52.109457+00:00 app[web.1]: I, [2021-11-16T10:26:52.109394 #4]  INFO -- : [a4419bcb-7f01-4e1f-9e14-1e1e20a97875] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:52 +0000
2021-11-16T10:26:52.110059+00:00 app[web.1]: I, [2021-11-16T10:26:52.110020 #4]  INFO -- : [a4419bcb-7f01-4e1f-9e14-1e1e20a97875] Processing by UsersController#user_home as HTML
2021-11-16T10:26:52.110091+00:00 app[web.1]: I, [2021-11-16T10:26:52.110068 #4]  INFO -- : [a4419bcb-7f01-4e1f-9e14-1e1e20a97875]   Parameters: {"id"=>"3"}
2021-11-16T10:26:52.115220+00:00 app[web.1]: I, [2021-11-16T10:26:52.115162 #4]  INFO -- : [a4419bcb-7f01-4e1f-9e14-1e1e20a97875] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:52.115322+00:00 app[web.1]: I, [2021-11-16T10:26:52.115296 #4]  INFO -- : [a4419bcb-7f01-4e1f-9e14-1e1e20a97875] Completed 302 Found in 5ms (ActiveRecord: 2.5ms | Allocations: 508)
2021-11-16T10:26:52.116244+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=a4419bcb-7f01-4e1f-9e14-1e1e20a97875 fwd="106.180.6.156" dyno=web.1 connect=0ms service=7ms status=302 bytes=1218 protocol=https
2021-11-16T10:26:52.339550+00:00 app[web.1]: I, [2021-11-16T10:26:52.339486 #4]  INFO -- : [9028936e-55b0-4226-ac12-559bbc25c903] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:52 +0000
2021-11-16T10:26:52.340154+00:00 app[web.1]: I, [2021-11-16T10:26:52.340112 #4]  INFO -- : [9028936e-55b0-4226-ac12-559bbc25c903] Processing by UsersController#user_home as HTML
2021-11-16T10:26:52.340178+00:00 app[web.1]: I, [2021-11-16T10:26:52.340158 #4]  INFO -- : [9028936e-55b0-4226-ac12-559bbc25c903]   Parameters: {"id"=>"3"}
2021-11-16T10:26:52.345418+00:00 app[web.1]: I, [2021-11-16T10:26:52.345357 #4]  INFO -- : [9028936e-55b0-4226-ac12-559bbc25c903] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:52.345523+00:00 app[web.1]: I, [2021-11-16T10:26:52.345494 #4]  INFO -- : [9028936e-55b0-4226-ac12-559bbc25c903] Completed 302 Found in 5ms (ActiveRecord: 2.4ms | Allocations: 514)
2021-11-16T10:26:52.346324+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=9028936e-55b0-4226-ac12-559bbc25c903 fwd="106.180.6.156" dyno=web.1 connect=0ms service=7ms status=302 bytes=1226 protocol=https
2021-11-16T10:26:52.570562+00:00 app[web.1]: I, [2021-11-16T10:26:52.570502 #4]  INFO -- : [94c6c97c-b2e7-4773-a20b-bedaf1703686] Started GET "/users/3/home" for 106.180.6.156 at 2021-11-16 10:26:52 +0000
2021-11-16T10:26:52.571219+00:00 app[web.1]: I, [2021-11-16T10:26:52.571118 #4]  INFO -- : [94c6c97c-b2e7-4773-a20b-bedaf1703686] Processing by UsersController#user_home as HTML
2021-11-16T10:26:52.571219+00:00 app[web.1]: I, [2021-11-16T10:26:52.571166 #4]  INFO -- : [94c6c97c-b2e7-4773-a20b-bedaf1703686]   Parameters: {"id"=>"3"}
2021-11-16T10:26:52.576540+00:00 app[web.1]: I, [2021-11-16T10:26:52.576486 #4]  INFO -- : [94c6c97c-b2e7-4773-a20b-bedaf1703686] Redirected to https://tsuku-heroku-first-app-0001.herokuapp.com/users/3/home
2021-11-16T10:26:52.576648+00:00 app[web.1]: I, [2021-11-16T10:26:52.576621 #4]  INFO -- : [94c6c97c-b2e7-4773-a20b-bedaf1703686] Completed 302 Found in 5ms (ActiveRecord: 2.7ms | Allocations: 522)
2021-11-16T10:26:52.577595+00:00 heroku[router]: at=info method=GET path="/users/3/home" host=tsuku-heroku-first-app-0001.herokuapp.com request_id=94c6c97c-b2e7-4773-a20b-bedaf1703686 fwd="106.180.6.156" dyno=web.1 connect=0ms service=8ms status=302 bytes=1226 protocol=https

自分で試したこと

・ログの確認、エラーが出ていないか調べた。302foundがリダイレクトを表すことについては判明した。「やけに表示数が多くないか?」とも思った。
・コードをシンプルに書き換えた。少なくともローカル環境では動いていたころに戻した。でもダメだった。
・ほぼ一日ググったがわからなかった。

0

1Answer

/users/3/home を開こうとして /users/3/home にリダイレクトされるのを繰り返していますね。

redirect_to("/users/#{session[:user_id]}/home") に何度も到達する処理になっているのではないでしょうか。他のコントローラーや routes も見ないと分かりませんが。

従来動いていたのはログイン状態だったからで、セッションが切れて非ログイン状態なり処理の流れが変わった結果動かなくなった可能性があります。

1Like

Comments

  1. @Tsuku

    Questioner
    ありがとうございます。

    そうなんです、おっしゃる通り何度もリダイレクトしているみたいなんです。

    「他のコントローラーや routes も見ないと分かりませんが。」
    とのことなので、編集して、それらも記載しますね。

    もしよければ、それも見ていただけると助かります。
  2. 追記ありがとうございます。 users#user_home で、 @user.administrator が偽であれば users#user_home にリダイレクトしていますね。 render("users/home") の間違いでは。

    def user_home
    @user = User.find_by(id: params[:id])
    if @user.administrator == true
    redirect_to("/administrator/home")
    else
    redirect_to("/users/#{@user.id}/home")
    end
    end
  3. 問題とは関係ありませんが、 users#login で if 文の前にも redirect_to があるせいで if 文に到達しなくなっていますね。 redirect_to は呼んだ時点で return と同じようにメソッドを抜けることに注意してください。
  4. @Tsuku

    Questioner
    ご返信ありがとうございます。
    renderにすべきではと回答していただきましたが、ログインしたユーザーそれぞれに対して異なるユーザーホームページを表示させなければいけないので、renderにはできないんですよね。

    redirect_toにして、コントローラ内のhomeメソッドを起動させなければならないのです。

    それから、if文の前にもredirect_toがある件ですが、これは記述ミスでもともと存在しない一文でした。誤解させてすみません。この一文がなかったとしても動かないんですよね。

    しばらくはエラーとの戦いになりそうです。
  5. > redirect_toにして、コントローラ内のhomeメソッドを起動させなければならないのです。

    どういうことでしょうか。まずブラウザで /users/3/home を開いたとき、 routes に

    get "users/:id/home" => "users#user_home"

    があることから、 UsersController の user_home メソッドが呼ばれますよね。その中で

    redirect_to("/users/#{@user.id}/home")

    することでブラウザは /users/3/home にリダイレクトされ、以下同じ処理が繰り返されてリダイレクトループに陥ります。リダイレクト先を間違えているのでは。そもそも UsersController に home メソッドは存在しないようです。

  6. @Tsuku

    Questioner
    ありがとうございます。
    @uasiさんにいろいろ指摘していただいて、自分をとことん疑っていろいろ見直した結果、原因が判明しました。

    loginメソッドにおける分岐で、もし結果がfalseだとuser_homeメソッドが呼ばれるのですが、そこでも全く同じ分岐を作ってしまっていたのでリダイレクトの重複が起こったみたいです。

    @uasiさん、遅い時間なのにいろいろ確認対象を提示していただきありがとうございました。駆け出しエンジニアであるのに加えて、初めての質問ということもあり、非常にわかりづらい質問の仕方だったと思います。

    いろいろと勉強になりました。あらためてお礼申し上げます。
  7. 解決したようでよかったです。 Rails は処理の流れが散らばりやすいので、もし次に質問されるときもコード全体を載せると回答が得やすいと思います。またシンプルにしたコードや試したこと、調べた内容も(それで解決しなかったとしても)ヒントになることがあるので載せてみてください。

Your answer might help someone💌