0
0

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 3 years have passed since last update.

ゲストログインおよびログアウトした際、The page you were looking for doesn't exist.と表示される

Posted at

#現状
ゲストログインおよびログアウトボタンを押すとThe page you were looking for doesn’t exist.と表示されました。最近になってこのような現象が起こり、それまでは通常通りできておりました。

#エラーメッセージ
スクリーンショット 2021-07-21 13.54.58.png

#該当するソースコード

app/views/devise/shared/_header.html.erb
<!DOCTYPE html>
<html>
  <head>
    <title>App</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>
<div class="container">
  <header class="blog-header py-3">
    <div class="row flex-nowrap justify-content-between align-items-center">
      <div class="col-4 text-center">
       <%= link_to image_tag("fresh.png",width:"120px",height:"65px"), root_path%> 
      </div> 
        <% if user_signed_in? %>
        <div class="col-4 d-flex justify-content-end align-items-center">
        <a class="btn btn-sm btn-outline-secondary" <%= link_to 'ログアウト', destroy_user_session_path, method: :delete %></a>
       <% else %>
       <div class="col-4 d-flex justify-content-end align-items-center">
         <a class="btn btn-sm btn-outline-secondary" href="/users/sign_up">新規登録</a>
         <a class="btn btn-sm btn-outline-secondary" href="/users/sign_in">ログイン</a>
         <a class="btn btn-sm btn-outline-secondary" <%= link_to 'ゲストログイン', users_guest_sign_in_path, method: :post %></a>
      <% end %>
      </div>
    </div>
  </header>
  <div class="nav-scroller py-1 mb-2">
    <nav class="nav d-flex justify-content-between">
    <% if controller_name != 'ways' %>
      <%=link_to '試験方法',ways_path,class:"p-2 link-secondary"%>
     <% else %>
     <%=link_to '試験方法',ways_path,class:"p-2 link-secondary",style:"color: blue;"%>
     <% end %> 
    <% if controller_name != 'words' %>
      <%=link_to '用語',words_path,class:"p-2 link-secondary"%>
    <% else %>
    <%=link_to '用語',words_path,class:"p-2 link-secondary",style:"color: blue;"%>
    <% end %>
    <% if controller_name != 'questions' %>
      <%=link_to 'クイズ問題',questions_path,class:"p-2 link-secondary"%>
      <% else %>
      <%=link_to 'クイズ問題',questions_path,class:"p-2 link-secondary",style:"color: blue;"%>
      <% end %>
    <% if controller_name != 'answers' %>
      <%=link_to 'Q&A',answers_path,class:"p-2 link-secondary"%>  
      <% else %>
      <%=link_to 'Q&A',answers_path,class:"p-2 link-secondary",style:"color: blue;"%> 
      <% end %>
      <div class="text-muted">
      <% if user_signed_in? %>
      <% if current_user.image.attached? %>
     <%=link_to image_tag(current_user.image,width:"30px",height:"30px",style:"border-radius: 50%"),"/users/#{current_user.id}" %>
    <% end %>
    <% if controller_name != 'users' %>
      <%=link_to "#{current_user.nickname}さん", "/users/#{current_user.id}", class:"p-2 link-secondary"%>
 <% else %>
      <%=link_to "#{current_user.nickname}さん", "/users/#{current_user.id}", class:"p-2 link-secondary",style:"color: blue;"%>
 <% end %>
  <% else %>
  <%= link_to 'FRESHとは',concretes_path,class:"p-2 link-secondary" ,style:"color: blue; font-size: 20px;"%>
  <% end %>
  </div>
   </nav>
  </div>
</div>
config/routes.rb
Rails.application.routes.draw do
  devise_for :users, controllers: {
    registrations: "users/registrations",
  }
  devise_scope :user do
    post 'users/guest_sign_in', to: 'users/sessions#new_guest'
  end
  root to: "concretes#index"
  resource :concretes, only: [:show]
  resources :users, only: [:show,:edit]
  resources :ways do
    collection do
      get 'search'
    end
  resources :likes, only: [:create, :destroy]
  resources :waycomments, only: [:create, :destroy]
  end
  resources :questions do
    collection do
      get 'search'
    end
    resources :choices, only: [:index,:create]
  end
  resources :answers do
    collection do
      get 'search'
    end
    resources :responses, only: [:create]
  end
  resources :words do
    collection do
      get 'search'
    end
  end
end

#原因
ターミナルに「tail -f production.log」を打ったところActiveRecord::RecordNotFound (Couldn't find User with 'id'=guest_sign_in):と表示され、本番環境用のデータベースにゲスト用のuser情報がなかったためです。

#解決方法
下記コードを記述し、ゲストログインボタンを押した段階でuserの情報を保存できるようにし、nginxを再起動したところ解決しました。

app/controllers/users/sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
  def new_guest
    user = User.find_or_create_by(email: 'guest@example.com') do |user|
    user.nickname = "ゲスト"
    user.password = SecureRandom.alphanumeric(10) + [*'a'..'z'].sample(1).join + [*'0'..'9'].sample(1).join
    user.text = "ゲストです。"
    end
    sign_in user
    redirect_to root_path
  end
end

#参考
https://qiita.com/toto_public/items/61dcd0fb8fcdcb335195

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?