首页 > 解决方案 > 更新方法中的 ActionController::ParameterMissing

问题描述

我有更新方法的 ParameterMissing 错误。我用patch触发这个方法,这里是命令

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

标签: ruby-on-rails

解决方案


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

我认为问题是数据是 JSON 内容,那么,你应该使用标题: Content-Type: application.json


推荐阅读