前提・実現したいこと
管理者権限を利用して各ユーザーの入力した情報を閲覧できるようにしたいと思っています。
そこでユーザーIDを取得して情報を得ようとしたのですが、以下のエラーが発生してユーザーIDが取得できません。
発生している問題・エラーメッセージ
ActiveRecord::RecordNotFound in Admin::EventsController#index Couldn't find User without an ID
該当のソースコード
Ruby
1【routes.rb】 2ails.application.routes.draw do 3 devise_for :users 4 root to: 'events#index' 5 6 #一般ユーザー用のルーティング 7 resources :users 8 resources :events 9 10 #管理者用のルーティング 11 namespace :admin do 12 resources :users 13 resources :events, only: [:index, :show] 14 15 end 16end
Ruby
1【rails routesで出てきたそれぞれのパス】 2Prefix Verb URI Pattern Controller#Action 3 new_user_session GET /users/sign_in(.:format) devise/sessions#new 4 user_session POST /users/sign_in(.:format) devise/sessions#create 5 destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy 6 new_user_password GET /users/password/new(.:format) devise/passwords#new 7 edit_user_password GET /users/password/edit(.:format) devise/passwords#edit 8 user_password PATCH /users/password(.:format) devise/passwords#update 9 PUT /users/password(.:format) devise/passwords#update 10 POST /users/password(.:format) devise/passwords#create 11 cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel 12 new_user_registration GET /users/sign_up(.:format) devise/registrations#new 13 edit_user_registration GET /users/edit(.:format) devise/registrations#edit 14 user_registration PATCH /users(.:format) devise/registrations#update 15 PUT /users(.:format) devise/registrations#update 16 DELETE /users(.:format) devise/registrations#destroy 17 POST /users(.:format) devise/registrations#create 18 root GET / events#index 19 users GET /users(.:format) users#index 20 POST /users(.:format) users#create 21 new_user GET /users/new(.:format) users#new 22 edit_user GET /users/:id/edit(.:format) users#edit 23 user GET /users/:id(.:format) users#show 24 PATCH /users/:id(.:format) users#update 25 PUT /users/:id(.:format) users#update 26 DELETE /users/:id(.:format) users#destroy 27 events GET /events(.:format) events#index 28 POST /events(.:format) events#create 29 new_event GET /events/new(.:format) events#new 30 edit_event GET /events/:id/edit(.:format) events#edit 31 event GET /events/:id(.:format) events#show 32 PATCH /events/:id(.:format) events#update 33 PUT /events/:id(.:format) events#update 34 DELETE /events/:id(.:format) events#destroy 35 admin_users GET /admin/users(.:format) admin/users#index 36 POST /admin/users(.:format) admin/users#create 37 new_admin_user GET /admin/users/new(.:format) admin/users#new 38 edit_admin_user GET /admin/users/:id/edit(.:format) admin/users#edit 39 admin_user GET /admin/users/:id(.:format) admin/users#show 40 PATCH /admin/users/:id(.:format) admin/users#update 41 PUT /admin/users/:id(.:format) admin/users#update 42 DELETE /admin/users/:id(.:format) admin/users#destroy 43 admin_events GET /admin/events(.:format) admin/events#index 44 admin_event GET /admin/events/:id(.:format) admin/events#show
Ruby
1【管理者側からユーザーの情報を閲覧するためのコントローラー】 2class Admin::EventsController < ApplicationController 3 before_action :if_not_admin 4 before_action :set_user, only: [:index, :show] 5 6 def index 7 end 8 9 def show 10 end 11 12 private 13 14 def if_not_admin 15 redirect_to root_path unless current_user.admin? 16 end 17 18 def set_user 19 @users = User.find(params[:id]) 20 @events = Event.all 21 end 22end
Ruby
1【各ユーザーのページに遷移するためのパスの記述】 2<% @users.each do |user| %> 3 <%= link_to user.name, admin_events_path(user) %> 4<% end %>
Ruby
1【各ユーザーのindex.html.erb】 2<% if current_user.admin? %> 3 <%# 管理者のみ見れる %> 4 <%= link_to "ユーザーリスト", admin_users_path %> 5 <%# // 管理者のみ見れる %> 6<% end %> 7 8<%= month_calendar events: @events, user: @users do |date, events, users| %> 9 <%= date.day %> 10 11 <%# 管理者用表示 %> 12 <% if current_user.admin? %> 13 <% events.each do |event| %> 14 <div class = "pain"> 15 <%= link_to event.pain.name, "#", class: "btn btn-success" %> 16 </div> 17 <% end %> 18 <%# // 管理者用表示 %> 19 <%# 一般ユーザー用表示 %> 20 <% else %> 21 <% events.each do |event| %> 22 <div class = "pain"> 23 <%= link_to event.pain.name, event_path(event.id), class: "btn btn-success" %> 24 </div> 25 <% end %> 26 <% end %> 27 <%# // 一般ユーザー用表示 %> 28 29<% end %> 30 31<div class = "new-button"> 32<%= link_to '経過観察', new_event_path %> 33</div>
試したこと
おそらくルーティングの記述がおかしいのかと思うのですが、正直、何が原因なのか分からなくなっている現状です。
補足情報(FW/ツールのバージョンなど)
ruby '2.6.5'
devise (4.8.1)
simple_calendar (2.4.3)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/07/17 07:29