LoginSignup
1
0

More than 3 years have passed since last update.

Users must exist エラーの解決例

Last updated at Posted at 2021-02-11

Users must exist
上記エラーは、ユーザーが存在していない。
また、正しくこの投稿にユーザーが紐づいていないという内容です。

posts_controller.rbのストロングパラメータで
ユーザーとの紐付けを明示している箇所があります。

posts_controller.rb
def post_params
 params.require(:post).permit(:title,:content).merge(user_id:current_user.id)

上記ファイルは正しく表記されているのでDBに保存する前に
問題が生じている可能性があります。したがってMVCに沿ってモデルを確認します。

models/post.rbの記述を確認するとbe longs_to :usersと記述されています。

be longs_toは、「あるデータにある一つのデータが紐づく」という意味です。
したがってusersをuserの単数形に修正します。

1
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
1
0