2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Couldn't find User with 'id'エラーについて

Last updated at Posted at 2020-03-02

はじめに

ブラウザにてviewページを表示しようとしたところ、表題のエラーとなりました。
Couldn't find User with 'id'エラー
idが見つかりませんよと言われてます。
原因を調べてみました。

今回の原因および解決法

以下の通りresourcesを複数形から単数形に変更したところ改善されました。

routes.rb(変更前)

Rails.application.routes.draw do
  resources :users,only: [:show,:index,:edit,:update]
  resources :books
  devise_for :users
end

routes.rb(変更後)

Rails.application.routes.draw do
  resource :user,only: [:show,:index,:edit,:update]
  resource :book
  devise_for :users
end

resourceメソッドには単数形のresourceと複数形のresources、
2つの定義が存在し、今回はresourceでpathを生成しないとURLにIDまで引き渡してしまう為?エラーとなっていたみたいです。

こちらの方法で改善されたのでCouldn't find User with 'id'エラーが出た際はresource部分を確認してみるといいと思います。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?