Railsアプリでユーザーのマイページを作っているのですが、
Couldn't find User without an ID エラーを解決できずにいます。
前提
: railsアプリのマイページを開発中です。
: 以下コードを参考にしています。
https://github.com/FarStep131/follow_app/blob/main/app/views/users/show.html.erb
: 上記リンクにはフォローボタンの処理などが書かれていますが、
自分はまだフォロー処理のコントローラーは作っていません。
: まずは前段階として、ユーザーの名前を取得、表示するだけのコードを書こうと
思っています。
実現したいこと
: 下記コードでuser_idを取得する想定です。
home_controller.rb
1@user = User.find(params[:user_id])
発生している問題・エラーメッセージ
ActiveRecord::RecordNotFound at /home/mypage
Couldn't find User without an ID
Request info
Request parameters
{"controller"=>"home", "action"=>"mypage"}
期待した処理結果
以下コードでユーザーの名前が表示できると思っていました。
mypage.html.erb
1<%= @user.name %>
ターミナルの表示
ActiveRecord::RecordNotFound - Couldn't find User without an ID:
app/controllers/home_controller.rb:37:in `mypage'
Started POST "/__better_errors/3ce4861ff68a121e/variables" for ::1 at 2022-10-17 16:34:15 +0900
Bookmark Load (1.0ms) SELECT bookmarks
.* FROM bookmarks
WHERE bookmarks
.user_id
= 7 /* loading for inspect */ LIMIT 11
↳ (eval):72:in `block in render'
該当のソースコード
Home.controller.rb
1def mypage 2 @bookmarks = Bookmark.where(user_id: current_user.id) 3 @user = User.find(params[:user_id]) 4 end 5
Mypage.html.erb
1 2<%= link_to 'topへ戻る', root_path %> 3<br><%= @user.name %> 4エラー原因: @user = User.find(params[:user_id]) 5
user.rb
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 5 6 devise :database_authenticatable, :registerable, 7 :recoverable, :rememberable, :validatable 8 has_many :posts, dependent: :destroy 9 has_many :comments 10 has_many :likes, dependent: :destroy 11 has_many :bookmarks, dependent: :destroy 12 13 # フォローをした、されたの関係 14 has_many :relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy 15 has_many :reverse_of_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy 16 17 # 一覧画面で使う 18 has_many :followings, through: :relationships, source: :followed 19 has_many :followers, through: :reverse_of_relationships, source: :follower 20 21 # フォローしたときの処理 22 def follow(user_id) 23 relationships.create(followed_id: user_id) 24 end 25 # フォローを外すときの処理 26 def unfollow(user_id) 27 relationships.find_by(followed_id: user_id).destroy 28 end 29 # フォローしているか判定 30 def following?(user) 31 followings.include?(user) 32 end 33end
routes.rb
1Rails.application.routes.draw do 2 get 'relationships/followings' 3 get 'relationships/followers' 4 mount RailsAdmin::Engine => '/admin', as: 'rails_admin' 5 get 'home/top' 6 devise_for :users 7 root to: "home#top" 8 get 'home/mypage' 9 get 'home/show/:id', :to => 'home#show' 10 resources :posts do #postsコントローラへのルーティング 11 resources :comments, only: [:create] #commentsコントローラへのルーティング 12 resource :likes, only: [:create, :destroy] 13 resource :bookmarks, only: [:create, :destroy] 14 end 15 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 16 resources :users do 17 resources :relationships, only: [:create, :destroy] 18 get 'followings' => 'relationships#followings', as: 'followings' 19 get 'followers' => 'relationships#followers', as: 'followers' 20 end 21end
自分で調べたことや試したこと
記事を3つ読んでそれぞれの解決策を試みましたが、いずれも解決に至りませんでした。
1つ目: ルーティングをresource→resourcesに書き直す
→書き直しましたが上記のエラーメッセージが変わらず表示されました。
https://qiita.com/kaichi_lvg/items/c5b8d594929ed8047966#%E6%8C%99%E5%8B%95%E7%A2%BA%E8%AA%8D
2つ目: Item.find(params[:id])→Item.find(params[:item_id]) に修正
→書き直しましたが上記のエラーメッセージが変わらず表示されました。
https://qiita.com/WI2rz/items/ae291c52af78ad0467f8
解決策3:findではなくfind_by メソッドで書く
:https://ichigick.com/record-not-found-without-id/
→なぜか今のユーザーネームではなく違うユーザーの名前が表示されました。
使っているツールのバージョンなど補足情報
パソコン:MacBook Pro
バージョン:rails6
その他何か足りない情報あればご指摘ください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。