0
0

More than 1 year has passed since last update.

ActiveRecord::RecordNotFound in UsersController#show エラーの解決方法!検証ツールを使えば...

Posted at

エラー内容

  • ActiveRecord::RecordNotFound in UsersController#showというエラーが表示された。

ActiveRecord RecordNotFound in UsersController show.png

仮説

  • idがshowアクションへ送られていないのでは?と思い、usersコントローラーの@user@user = User.find_by(id: params[:user_id])にしてみたが、別のnilエラーになってしまう。問題なかった
  • ルーティング間違いかと思い、以下も確認したが、問題なさそう
  root to: 'prototypes#index'
  resources :prototypes, only: [:new, :create, :show, :edit, :update, :destroy] do
   resources :comments, only: :create
  end
  resources :users, only: :show
  • ログイン中のユーザーは以下のように設定したことで、しっかりと遷移されている。ここも問題なさそう。
  こんにちは、<%= link_to "#{current_user.name}さん", user_path(id: current_user.id), class: :greeting__link%>

解決手順

  • まずはブラウザの検証ツール使ってみる。すると、abcさんを選択してもuser8となっていることがわかる。

  • Viewである_prototype.html.erbをみてみると、以下がprototypeのidになっている

    • なので、本来userのidである1が呼び出されるべきなのにprototypeのidが呼び出されてしまっているのだ!
  • 修正前:

protospace-37383/app/views/prototypes/_prototype.html.erb
   <%= link_to "by  #{prototype.user.name}", user_path(prototype.id), class: :card__user %>
  • 修正後:
protospace-37383/app/views/prototypes/_prototype.html.erb
   <%= link_to "by  #{prototype.user.name}", user_path(prototype.user.id), class: :card__user %>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0