前提・実現したいこと
ブラウザでログアウトのところをクリックして、ログイン画面に遷移できるようにしたい。
発生している問題・エラーメッセージ
post一覧画面です。ここからログアウトしようとしました。
ログアウトしたきのサーバーログです。
一応成功はしてるみたいですが、
tarted GET "/posts" for ::1 at 2021-12-09 03:52:46 +0900 Processing by PostsController#index as HTML User Load (1.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 4], ["LIMIT", 1]] ↳ app/controllers/application_controller.rb:9 Rendering posts/index.html.erb within layouts/application Post Load (0.5ms) SELECT "posts".* FROM "posts" ↳ app/views/posts/index.html.erb:13 Rendered posts/index.html.erb within layouts/application (11.1ms) Rendered shared/_header.html.erb (6.2ms) Rendered shared/_before_login_header.html.erb (0.6ms) Rendered shared/_flash_message.html.erb (1.5ms) Rendered shared/_footer.html.erb (0.3ms) Completed 200 OK in 355ms (Views: 147.7ms | ActiveRecord: 7.3ms)
該当のソースコード
contorollers/posts_contoroller.rb
class PostsController < ApplicationController before_action :require_login, only: [:edit, :destroy] before_action :set_post, only: %i[show edit update destroy] # GET /posts def index @posts = Post.all end # GET /posts/1 def show; end # GET /posts/new def new @post = Post.new end # GET /posts/1/edit def edit; end # POST /posts def create @post = Post.new(post_params) if @post.save redirect_to @post, notice: 'Post was successfully created.' else render :new end end # PATCH/PUT /posts/1 def update if @post.update(post_params) redirect_to @post, notice: 'Post was successfully updated.' else render :edit end end # DELETE /posts/1 def destroy @post.destroy redirect_to posts_url, notice: 'Post was successfully destroyed.' end private # Use callbacks to share common setup or constraints between actions. def set_post @post = Post.find(params[:id]) end # Only allow a trusted parameter "white list" through. def post_params params.require(:post).permit(:title, :content) end end
contorollers/user_sessions_controller.rb
class UserSessionsController < ApplicationController def new; end def create @user = login(params[:email], params[:password]) if @user redirect_back_or_to root_path, success: 'Login successful' else flash.now[:danger] = 'I failed Login' render :new end def destroy logout redirect_to root_path, notice: 'logout successful' end end end
app/contorollers/aplication_contoroller.rb
class ApplicationController < ActionController::Base before_action :current_user before_action :require_login protect_from_forgery with: :null_session private def current_user @current_user ||= User.find_by(id: session[:user_id]) if session[:user_id] end protected def not_authenticated redirect_to login_path, alert: "Please login first" end end
app/contorollers/posts_contoroller.rb
class PostsController < ApplicationController before_action :require_login, only: [:edit, :destroy] before_action :set_post, only: %i[show edit update destroy] # GET /posts def index @posts = Post.all end # GET /posts/1 def show; end # GET /posts/new def new @post = Post.new end # GET /posts/1/edit def edit; end # POST /posts def create @post = Post.new(post_params) if @post.save redirect_to @post, notice: 'Post was successfully created.' else render :new end end # PATCH/PUT /posts/1 def update if @post.update(post_params) redirect_to @post, notice: 'Post was successfully updated.' else render :edit end end # DELETE /posts/1 def destroy @post.destroy redirect_to posts_url, notice: 'Post was successfully destroyed.' end private # Use callbacks to share common setup or constraints between actions. def set_post @post = Post.find(params[:id]) end # Only allow a trusted parameter "white list" through. def post_params params.require(:post).permit(:title, :content) end end
posts/index.html.rb
<h1>Posts</h1> <table> <thead> <tr> <th>Title</th> <th>Content</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @posts.each do |post| %> <tr> <td><%= post.title %></td> <td><%= post.content %></td> <td><%= link_to 'Show', post %></td> <td><%= link_to 'Edit', edit_post_path(post) %></td> <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody> </table> <br> <%= link_to 'New Post', new_post_path %>
shared/_header.html.erb
<header> <nav class="navbar navbar-expand-lg navigation navbar-light bg-light"> <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav ml-auto main-nav align-items-center"> <li class="nav-item dropdown dropdown-slide"> <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Post </a> <div class="dropdown-menu dropdown-menu-right"> <%= link_to 'Post一覧', posts_path, class: 'dropdown-item' %> <%= link_to 'Post作成', new_post_path, class: 'dropdown-item' %> </div> </li> <li class="nav-item"> <%= link_to 'Like一覧', '#', class: 'nav-link' %> </li> <li class="nav-item dropdown dropdown-slide"> <a href="#" class="nav-link" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="header-profile"> <%= image_tag 'sample', size: '40x40', class: 'rounded-circle mr15'%> </a> <div class="dropdown-menu dropdown-menu-right"> <div class="dropdown-item"><%# "#{current_user.last_name} #{current_user.first_name}" %></div> <div class="dropdown-divider"></div> <%= link_to 'プロフィール', '#', class: 'dropdown-item' %> <%= link_to 'ログアウト', '#', class: 'dropdown-item' %> </div> </li> </ul> </div> </nav> </header>
shared/_before_login_header.html.erb
<header> <nav class="navbar navbar-expand-lg navigation navbar-light bg-light"> <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav ml-auto main-nav align-items-center"> <li class="nav-item"> <%= link_to 'ログイン', login_path, class: 'nav-link' %> </li> </ul> </div> </nav> </header>
shared/_flash_message.html.erb
<% flash.each do |message_type, message| %> <div class="alert alert-<%= message_type %>"><%= message %></div> <% end %>
shared/_footer.html.erb
<footer id='footer' class="footer-bottom"> <div class="container"> <div class="row"> <div class="col-sm-6 col-12"> <div class="copyright"> <p>Copyright © 2021. RUNTEQ</p> </div> </div> </div> </div> </footer>
試したこと
通ってきたルートのアクションを確認
補足情報(FW/ツールのバージョンなど)
pc: mac book air
あなたの回答
tips
プレビュー