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

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

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

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby on Rails

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

Q&A

解決済

1回答

1627閲覧

deviseのサインアウトでエラー

退会済みユーザー

退会済みユーザー

総合スコア0

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby on Rails

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

0グッド

1クリップ

投稿2019/11/06 13:14

編集2019/11/07 12:16

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' %>

お気づきの点がありましたら、ご指摘ください。
よろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

正攻法ではありませんが....一応これで解決しそうな気はします

【解決策】deviseのログアウト機能でのエラー


[備考]

なぜかわかりませんが、viewに書いている method: :delete が実際には反映されていないのが根本的な原因のように思われます。

が、今の状況であれば、この問題の解決をするのは後回しにしてもよい気もします。

投稿2019/11/10 02:29

siruku6

総合スコア1382

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問