ruby-on-rails - 更新方法中的 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
解决方案
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
推荐阅读
- mysql - 使用 Sequelize 在 Feathers.js 中连接到多个 MySQL 数据库
- python - TypeError:“int”对象不可调用(生成器错误)
- javascript - MongoDB意外崩溃流星
- reactjs - 页面已加载时,React Redux 不会更改道具
- .net - UdpClient 校验和如何工作
- vba - VBS 和 Macro 之间 QueryTables.add 的区别
- angular - 在 Angular 7 应用程序中与 facebook 连接
- postgresql - 在创建 docker 容器时导入数据库 - 需要使用 pg_restore
- typescript - 如何在本机反应中使用带有 map() 的 Promise
- macos - dnsmasq 如何在 Mac OS 上使用 .localhost 而不是 Chrome 中的 .dev