ActionController::ParameterMissing in the update method

65 views Asked by At

I have ParameterMissing error for the update method. I used patch to trigger this method, here is the command

curl -i -X PATCH 'localhost:3000/products/1' -d '{"product":{ "title": "t", "price": "1.23", "count": "3"}}'

Product_controller.rb

def edit
  @product = Product.find(params[:id])
end

def update
  # use update method
  @product = Product.find(params[:id])

  # use update_attributes to update the record
  if @product.update_attributes(product_params)
    render json: { status: :ok, message: 'Product updated ', data: @product }
  else
    render json: { status: :error, message: 'Product not available', data: @product }
  end
end

private
  def product_params
    params.require(:product).permit(:title, :price, :count)
  end
2

There are 2 answers

0
William Tran On

curl -i -X PATCH 'localhost:3000/products/1' -d '{"product":{ "title": "t", "price": "1.23", "count": "3"}}' -H 'Content-Type: application/json'

I think the problem is data is JSON content, then, you should use header: Content-Type: application.json

0
barmic On

Try this:

curl -i -X PATCH 'localhost:3000/products/1' -d "product[title]=t" -d "product[price]=1.23" -d "product[count]=3" -H 'Content-Type: application/json'

If you test your application, Postman would be more comfortable choice.