LoginSignup
0
0

More than 1 year has passed since last update.

【Rails】ActiveRecord::RecordNotFound (Couldn't find Order without an ID)の対処法

Posted at

症状

RailsAPIでメソッドを作成していたところ、以下のエラーメッセージが表示されてしまいました。
翻訳すると、「IDのないHogeが見つかりませんでした」

error
Completed 404 Not Found in 1ms (ActiveRecord: 0.0ms | Allocations: 158)

ActiveRecord::RecordNotFound (Couldn't find Hoge without an ID)

メソッドにパラメータは以下です。

parameter
Parameters: {"params"=>{"id"=>12}, "hoge_id"=>"12", "hoge"=>{}}

該当のメソッドは以下です。

hoge.rb
class OrdersController < ApplicationController
  def update
    hoge= Hoge.find(params[:id])

    render json: {
      hoge: hoge
    }, status: :ok
  end
end

解決方法

パラメータを適切に受け取れるように、paramsの指定先を修正したら解決しました。
パラメータの受け取り方がよくなく、結果、DBにfindしようとしたときに、id指定ができていなかったため、「IDがないよ」と怒られていたようです。

hoge.rb
class OrdersController < ApplicationController
  def update
    hoge= Hoge.find(params[:hoge_id])

    render json: {
      hoge: hoge
    }, status: :ok
  end
end

参考

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