deviseを利用してログイン機能を利用してログアウトをしたところ、「Couldn't find User with 'id'=sign_out」というエラーが表示されました。
devise_for :userをroutesの先頭に記述したり、application.jsファイルを確認したりもしましたが、解決できませんでした。
以下エラー文と各コードになります。
ActiveRecord::RecordNotFound in UsersController#show Couldn't find User with 'id'=sign_out Extracted source (around line #8): 7 def show 8 @user = User.find(params[:id]) ←エラー表示部分 9 end 10 11 def edit
application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. JavaScript code in this file should be added after the last require_* statement. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require jquery_ujs //= require turbolinks //= require bootstrap-sprockets //= require_tree .
routes.rb
Rails.application.routes.draw do devise_for :users get root 'top#top' get '/home/about', to: 'top#home' resources :users resources :books # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
users_controller.rb
class UsersController < ApplicationController def index @user = current_user @users = User.all end def new @user = User.new end def create user = User.new(user_params) user.save redirect_to '/books/:id' end def show @user = User.find(params[:id]) end def edit @user = User.find(params[:id]) end private def user_params params.require(:user).permit(:title, :opinion, :profile_image) end end
user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attachment :profile_image end
_header.html.erb
<header class="navbar navbar-inverse navbar-fixed-top"> <div class="container header-container"> <div class="navbar-left"> <h1>Bookers</h1> </div> <ul class="nav navbar-nav navbar-right"> <% if user_signed_in? %> <li> <%= link_to user_path(current_user) do %> <i class="fa fa-home">Home</i> <% end %> </li> <li> <%= link_to users_path do %> <i class="fa fa-user">Users</i> <% end %> </li> <li> <%= link_to books_path do %> <i class="fa fa-book">Books</i> <% end %> </li> <li> <%= link_to destroy_user_session_path, method: :delete do %> <i class="fa fa-log-out">logout</i> <% end %> </li> <% else %> <li> <%= link_to root_path do %> <i class="fa fa-home">Home</i> <% end %> </li> <li> <%= link_to home_about_path do %> <i class="fa fa-link">About</i> <% end %> </li> <li> <%= link_to new_user_registration_path do %> <i class="fa fa-edit">sign_up</i> <% end %> </li> <li> <%= link_to new_user_session_path do %> <i class="fa fa-log-in">login</i> <% end %> </li> <% end %> </ul> </div> </header>
show.html.erb
<%= render 'shared/header' %> <div class="top"> <div class="container"> <div class="row"> <div class="col-lg-3"> <p>User info</p> <%= attachment_image_tag @user, :profile_image, :fill, 30, 30, format: 'jpeg', fallback: "no_image.jpg" %> <table class="table"> <tr> <th>name</th> <th><%= @user.name %></th> </tr> <tr> <th>introduction</th> <th><%= @user.introduction %></th> </tr> </table> <%= link_to edit_user_path, class: "btn btn-default" do %> <i class="fa fa-wrench"></i> <% end %> <p>New book</p> <%= form_for(@user, url: '/users') do |f| %> <p>Title</p> <%= f.text_field :title %> <p>Opinion</p> <%= f.text_area :opinion %> <%= f.submit 'Create Book' %> <% end %> </div> <div class="col-lg-9"> <p>Books</p> <table class="table table-hover"> <thead> <tr> <th></th> <th>Title</th> <th>Opinion</th> </tr> </thead> <tbody> <% if @users.present? %> <% @users.each do |user| %> <tr> <td></td> <td><%= user.title %></td> <td><%= user.opinion %></td> </tr> <% end %> <% end %> </tbody> </table> </div> </div> </div> </div> <%= render 'shared/footer' %>
お気づきの点がありましたら、ご指摘ください。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。