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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

Q&A

解決済

1回答

855閲覧

ログアウト機能について

tetopeg

総合スコア4

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

1グッド

0クリップ

投稿2022/12/04 13:08

編集2022/12/05 08:34

前提

ログアウトのリンクを押しても、ログアウト状態にならないのと画面遷移しないです。
時間が経つとなぜかログアウト状態になります。 deviseを使っているのですがログイン、新規登録はしっかりできます。

実現したいこと

リンクをおせばログアウトしてトップ画面に遷移させたい

発生している問題・エラーメッセージ

よくあるルーティングエラーなどのエラーメッセージは表示されないです。
↓の3行目の箇所でなぜかユーザーコントローラが呼び出されていて理由が分からないです

Cloud9のターミナル

1Started GET "/users/sign_out" for 115.36.216.99 at 2022-12-04 12:49:43 +0000 2Cannot render console from 115.36.216.99! Allowed networks: 127.0.0.0/127.255.255.255, ::1 3Processing by UsersController#show as HTML 4 Parameters: {"id"=>"sign_out"} 5 Rendering layout layouts/application.html.erb 6 Rendering users/show.html.erb within layouts/application 7 Rendered users/show.html.erb within layouts/application (Duration: 0.1ms | Allocations: 56) 8[Webpacker] Everything's up-to-date. Nothing to do 9 (0.1ms) SELECT sqlite_version(*) 10 ↳ app/views/layouts/application.html.erb:15 11 User Load (4.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]] 12 ↳ app/views/layouts/application.html.erb:15 13 Rendered layout layouts/application.html.erb (Duration: 39.8ms | Allocations: 9076) 14Completed 200 OK in 51ms (Views: 49.3ms | ActiveRecord: 5.1ms | Allocations: 10411)

該当のソースコード

application_controller.rb

1class ApplicationController < ActionController::Base 2 before_action :configure_permitted_parameters, if: :devise_controller? 3 4 def after_sign_in_path_for(resource) 5 user_path(resource) 6 end 7 8 def after_sign_out_path_for(resource) 9 byebug 10 root_path 11 end 12 13 protected 14 15 def configure_permitted_parameters 16 devise_parameter_sanitizer.permit(:sign_up, keys:[:name]) 17 devise_parameter_sanitizer.permit(:sign_in, keys:[:name]) 18 end 19 20end

routes.rb

1Rails.application.routes.draw do 2 devise_for :users 3 root to: 'homes#top' 4 get 'home/about' => "homes#about", as: "about" 5 resources :books, only:[:new, :index, :show, :edit, :create, :update, :destroy] 6 resources :users, only:[:index, :show, :edit, :update] 7 8 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 9end

application.html.erb

1<!DOCTYPE html> 2<html> 3 <head> 4 <title>復習</title> 5 <meta name="viewport" content="width=device-width,initial-scale=1"> 6 <%= csrf_meta_tags %> 7 <%= csp_meta_tag %> 8 9 <%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> 10 <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> 11 </head> 12 13 <body> 14 <header> 15 <% if user_signed_in? %> 16 <li> 17 <%= link_to "ログアウト",destroy_user_session_path, method: :delete %> 18 </li> 19 <% else %> 20 <li> 21 <%= link_to "新規登録",new_user_registration_path %> 22 </li> 23 <li> 24 <%= link_to "ログイン",new_user_session_path %> 25 </li> 26 <% end %> 27 </header> 28 <%= yield %> 29 <footer> 30 31 </footer> 32 </body> 33</html>

Gemfile

1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '3.1.2' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' 7gem 'rails', '~> 6.1.7' 8# Use sqlite3 as the database for Active Record 9gem 'sqlite3', '~> 1.4' 10# Use Puma as the app server 11gem 'puma', '~> 5.0' 12# Use SCSS for stylesheets 13gem 'sass-rails', '>= 6' 14# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 15gem 'webpacker', '~> 5.0' 16# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 17gem 'turbolinks', '~> 5' 18# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 19gem 'jbuilder', '~> 2.7' 20# Use Redis adapter to run Action Cable in production 21# gem 'redis', '~> 4.0' 22# Use Active Model has_secure_password 23# gem 'bcrypt', '~> 3.1.7' 24 25# Use Active Storage variant 26# gem 'image_processing', '~> 1.2' 27 28# Reduces boot times through caching; required in config/boot.rb 29gem 'bootsnap', '>= 1.4.4', require: false 30 31group :development, :test do 32 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 33 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 34end 35 36group :development do 37 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 38 gem 'web-console', '>= 4.1.0' 39 # Display performance information such as SQL time and flame graphs for each request in your browser. 40 # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md 41 gem 'rack-mini-profiler', '~> 2.0' 42 gem 'listen', '~> 3.3' 43 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 44 gem 'spring' 45end 46 47group :test do 48 # Adds support for Capybara system testing and selenium driver 49 gem 'capybara', '>= 3.26' 50 gem 'selenium-webdriver', '>= 4.0.0.rc1' 51 # Easy installation and use of web drivers to run system tests with browsers 52 gem 'webdrivers' 53end 54 55# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 56gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 57 58#以下追加 59gem 'devise' 60 61gem 'net-smtp' 62 63gem 'rails-i18n'

package.json

1{ 2 "name": "bookers2", 3 "private": true, 4 "dependencies": { 5 "@rails/actioncable": "^6.0.0", 6 "@rails/activestorage": "^6.0.0", 7 "@rails/ujs": "^6.0.0", 8 "@rails/webpacker": "5.4.3", 9 "bootstrap": "4.5", 10 "jquery": "^3.6.1", 11 "popper.js": "^1.16.1", 12 "turbolinks": "^5.2.0", 13 "webpack": "^4.46.0", 14 "webpack-cli": "^3.3.12" 15 }, 16 "version": "0.1.0", 17 "devDependencies": { 18 "webpack-dev-server": "^3" 19 } 20} 21

application.js

1// This file is automatically compiled by Webpack, along with any other files 2// present in this directory. You're encouraged to place your actual application logic in 3// a relevant structure within app/javascript and only use these pack files to reference 4// that code so it'll be compiled. 5 6import Rails from "@rails/ujs" 7import Turbolinks from "turbolinks" 8import * as ActiveStorage from "@rails/activestorage" 9import "channels" 10import "jquery"; 11import "popper.js"; 12import "bootstrap"; 13import "../stylesheets/application" 14 15Rails.start() 16Turbolinks.start() 17ActiveStorage.start()

試したこと

スペルミスや全角スペースがないかの確認
rails routesでルートパスの確認

補足情報(FW/ツールのバージョンなど)

Rails 6.1.4

shinoharat👍を押しています

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

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

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

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

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

tetopeg

2022/12/05 02:01

前に質問したものなんですが、削除リクエストを送ってしまい現在編集することができないです。
shinoharat

2022/12/05 07:07

ログの1行目に > Started GET "/users/sign_out" ... と表示されていますが、ログアウトするには「GET」ではなく「DELETE」でリクエストを送る必要があります。 HTTPメソッドが「GET」であることから、ログアウト用の link_to に付けている `method: :delete` オプションが有効に働いていないことが原因と推測されます。
shinoharat

2022/12/05 07:44

もしかしたら「@rails/ujs」というライブラリが有効になっていないんじゃないかなと思います。 - Gemfile - package.json - app/javascript/packs/application.js 辺りを質問文に追加してもらえませんか?
tetopeg

2022/12/05 08:39

ご指摘ありがとうございます。 ターミナルのリクエストの違いに気付けませんでした。 今該当するファイルをアップさせていただきました。Gemfileは以下追加の部分しか編集してないのと、application.haはbootstrapを導入するために、10から13行目を追加しております。 package.jsonはどこも触っておりません。 今までと同じとおりにすすめたつもりなので、間違いに気付けなかったです。
tetopeg

2022/12/05 08:40

ただ、bootstrapもうまく導入されてないのでそこは、また別の質問でしようと思っていました。
shinoharat

2022/12/05 09:14

質問文の編集ありがとうございます。 変更部分の情報も助かります🙏 > application.haはbootstrapを導入するために、10から13行目を追加しております。 そこが原因かもしれないので、一旦10から13行目をコメントアウトして、ログアウトが動作するか確認してもらって良いですか?
tetopeg

2022/12/05 10:36

shinoharatさまに質問したいのですが、問題自体は以下記載内容の通り解決できたのですが、config/initializers/devise.rb を変更する前に、ご指摘いただいたapplication.jsをコメントアウトしたらログアウトできたので、そこの原因を知りたいです。
shinoharat

2022/12/07 07:52

返答が遅くなってすみません。 > ご指摘いただいたapplication.jsをコメントアウトしたらログアウトできたので、そこの原因を知りたいです。 まず、最初に書いた通り、ログアウトするには「GET」ではなく「DELETE」でリクエストを送る必要があります。 link_to に `method: :delete` オプションを付けているのにそれが効いてないので、「rails/ujs」に問題があると推測できます。 「rails/ujs」は以下のような javascript で有効化できます。 --------------------- import Rails from "@rails/ujs" Rails.start() --------------------- application.js を拝見し、もしかすると追加した import のどれかに失敗しているせいで、「Rails.start()」までたどり着いていないのではないかと推測を立てました。 --------------------- import Rails from "@rails/ujs" // ↓ 追加した import のどれかがエラーになっている? import "jquery"; import "popper.js"; import "bootstrap"; import "../stylesheets/application" // ↓ import のエラーでコードが中断されるので start までたどり着かない? Rails.start() ---------------------
shinoharat

2022/12/07 08:08

気が向いたら、追加部分の import を消した状態でHTTPメソッドを確認してみてください。
tetopeg

2022/12/09 11:47

shinoharatさまご回答いただきありがとうございます。よく分かりました。とても丁寧に教えていただきありがとうございました。 気が向いたら、追加部分の import を消した状態でHTTPメソッドを確認してみてください。→こちら理解度を深めるためにも実施いたします。
guest

回答1

0

自己解決

ログイン認証で自分はユーザーネームとパスワードを使おうと思っていたのですが、
config/initializers/devise.rb のconfig.authentication_keys = [:email] をnameに変更することを忘れており、こちらを変更したら全て解決いたしました。
ご指摘いただいた、matherさまとshinoharatさま ありがとうございました。
matherさまからご指摘いただいたroutes.rbを記述するときの注意事項はこれからもでてきそうなエラーにつながりそうなので気を付けたいと思います。

投稿2022/12/05 10:31

tetopeg

総合スコア4

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問