実現したいこと
Webアプリ内に設置したログインフォームからのログイン/ログアウト機能を実現したいです。また、そのために以下のエラーメッセージを解決したいです。
発生している問題・エラーメッセージ
以下のようなエラーメッセージが出てきました。
どうやらcurrent_staff_member
に問題があるらしいです。
エラーメッセージは、次の画面遷移の際に現れました。
動きとしては画面のログインというボタンを押すと、そのボタンがログアウトという表記に切り替わるようにしたいです。
ですがログインボタンを押すと500エラーを吐きます。
terminal
1bash-4.4$ bin/rails s -e production -b 0.0.0.0 2=> Booting Puma 3=> Rails 6.0.2.1 application starting in production 4=> Run `rails server --help` for more startup options 5Puma starting in single mode... 6* Version 4.3.1 (ruby 2.6.4-p104), codename: Mysterious Traveller 7* Min threads: 5, max threads: 5 8 9* Environment: production 10* Listening on tcp://0.0.0.0:3000 11Use Ctrl-C to stop 12Error during failsafe response: undefined local variable or method `current_staff_member' for #<#<Class:0 13x0000561e60cf11e0>:0x0000561e61b6d070> 14Did you mean? current_page? 15 /apps/baukis2/app/views/staff/shared/_header.html.erb:4:in `_app_views_staff_shared__header_html_erb__4 16241832495033121032_47344236416940' 17 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/base.rb:274:in `_run' 18 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/template.rb:185:in `block in render' 19 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/notifications.rb:182:in `instrument' 20 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/template.rb:386:in `instrument_render_templat 21e' 22 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/template.rb:183:in `render' 23 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/partial_renderer.rb:358:in `block in 24 render_partial' 25 26 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/abstract_renderer.rb:89:in `block in 27 instrument' 28 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/notifications.rb:180:in `block in instr 29ument' 30 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/notifications/instrumenter.rb:24:in `in 31strument' 32 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/notifications.rb:180:in `instrument' 33 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/abstract_renderer.rb:88:in `instrume 34nt' 35 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/partial_renderer.rb:347:in `render_p 36artial' 37 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/partial_renderer.rb:317:in `render' 38 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/renderer.rb:65:in `render_partial_to 39_object' 40/usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/renderer.rb:53:in `render_partial' 41 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/helpers/rendering_helper.rb:38:in `render' 42 /apps/baukis2/app/views/layouts/staff.html.erb:14:in `_app_views_layouts_staff_html_erb__44324389533601 4319783_47344236385500' 44 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/base.rb:274:in `_run' 45 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/template.rb:185:in `block in render' 46 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/notifications.rb:182:in `instrument' 47 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/template.rb:386:in `instrument_render_templat 48e' 49 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/template.rb:183:in `render' 50 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/template_renderer.rb:70:in `render_w 51ith_layout' 52 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/template_renderer.rb:57:in `render_t 53emplate' 54 55 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/template_renderer.rb:13:in `render' 56 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/renderer.rb:61:in `render_template_t 57o_object' 58 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/renderer/renderer.rb:29:in `render_to_object' 59 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/rendering.rb:118:in `block in _render_templat 60e' 61 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/base.rb:304:in `in_rendering_context' 62 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/rendering.rb:117:in `_render_template' 63 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/streaming.rb:219:in `_render_temp 64late' 65 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/rendering.rb:103:in `render_to_body' 66 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/rendering.rb:52:in `render_to_bod 67y' 68 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/renderers.rb:142:in `render_to_bo 69dy' 70/usr/local/bundle/gems/actionpack-6.0.2.1/lib/abstract_controller/rendering.rb:25:in `render' 71 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/rendering.rb:36:in `render' 72 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/instrumentation.rb:44:in `block ( 732 levels) in render' 74 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/core_ext/benchmark.rb:14:in `block in m 75s' 76 /usr/local/lib/ruby/2.6.0/benchmark.rb:308:in `realtime' 77 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/core_ext/benchmark.rb:14:in `ms' 78 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/instrumentation.rb:44:in `block i 79n render' 80 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/instrumentation.rb:85:in `cleanup 81_view_runtime' 82 /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/railties/controller_runtime.rb:39:in `cle 83 84anup_view_runtime' 85 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/instrumentation.rb:43:in `render' 86 /apps/baukis2/app/controllers/concerns/error_handlers.rb:21:in `rescue500' 87 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/rescuable.rb:92:in `call' 88 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/rescuable.rb:92:in `rescue_with_handler 89' 90 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/rescuable.rb:165:in `rescue_with_handle 91r' 92 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/rescue.rb:25:in `rescue in proces 93s_action' 94 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/rescue.rb:21:in `process_action' 95 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/instrumentation.rb:33:in `block i 96n process_action' 97 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/notifications.rb:180:in `block in instr 98ument' 99/usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/notifications/instrumenter.rb:24:in `in 100strument' 101 /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/notifications.rb:180:in `instrument' 102 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/instrumentation.rb:32:in `process 103_action' 104 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal/params_wrapper.rb:245:in `process 105_action' 106 /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/railties/controller_runtime.rb:27:in `pro 107cess_action' 108 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/abstract_controller/base.rb:136:in `process' 109 /usr/local/bundle/gems/actionview-6.0.2.1/lib/action_view/rendering.rb:39:in `process' 110 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal.rb:191:in `dispatch' 111 /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_controller/metal.rb:236:in `block in action' 112…
該当のソースコード
current_staff_member
に関する処理を行うソースコードを以下に添付します。
このプログラムはcurrent_staff_member
の定義です。また、ヘルパーメソッドとして使えるようになっています。
ruby
1class Staff::Base < ApplicationController 2 private def current_staff_member 3 if session[:staff_member_id] 4 @current_staff_member ||= StaffMember.find_by(id: session[:staff_member_id]) 5 end 6 end 7 8 helper_method :current_staff_member 9end
以上で定義したメソッドを以下のHTML内で用います。このファイルの拡張はhtml.erb
です。
HTML
1<header> 2 <span class="logo-mark">BAUKIS2</span> 3 <%= 4 if current_staff_member 5 link_to "ログアウト", :staff_session, method: :delete 6 else 7 link_to "ログイン", :staff_login 8 end 9 %> 10</header>
ルーティングは以下のようになっています。
ruby
1Rails.application.routes.draw do 2 namespace :staff do 3 root "top#index" 4 get "login" => "sessions#new", as: :login 5 post "session" => "sessions#create", as: :session 6 delete "session" => "sessions#destroy" 7 end 8end 9
試したこと
エラーメッセージにはDid you mean? current_page?
とあったため、
HTML内のif current_staff_member
をcurrent_page?(current_staff_member)
としてみましたが結果は変わりませんでした。
補足情報(FW/ツールのバージョンなど)
Docker version 19.03.5
Rails 6.0.2.1
ruby 2.6.4p104 (2019-08-28 revision 67798) [x86_64-linux-musl]
不足している情報などありましたら教えて頂けると幸いです。どうぞ宜しくお願いいたします。
この内容は“Ruby on Rails 6 実践ガイド: 現場のプロから学ぶ本格Webプログラミング“のChapter7-3のセッションというセクションです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/20 14:26
2020/02/20 14:53
2020/02/20 16:53
2020/02/21 09:24