我有以下路线:
match '/curl_example' => 'people#curl_post_example', via: :post
在我的控制器中:
class PeopleController < ApplicationController
before_action :set_person, only: [:show, :edit, :update, :destroy]
skip_before_action :verify_authenticity_token, :only => [:curl_post_example]
...
def curl_post_example
Person.create(request.body.read)
render plain: "Thanks for sending a POST request with cURL! Payload: #{request.body.read}"
end
...
我正在尝试通过curl命令使用以下命令创建一个人...
curl -X POST -d "{"first_name"=>"mista", "last_name"=>"wintas"}" http://localhost:3000/curl_example
当我这样做时,我在控制台上收到此错误...
Started POST "/curl_example" for ::1 at 2016-11-08 13:24:46 -0500
Processing by PeopleController#curl_post_example as */*
Parameters: {"{first_name"=>">mista, last_name=>wintas}"}
Completed 500 Internal Server Error in 0ms (ActiveRecord: 0.0ms)
ArgumentError (When assigning attributes, you must pass a hash as an argument.):
对我来说看起来像是一个哈希?我想念什么?
您需要像这样传递参数:
curl -d "person[first_name]=john" -d "person[last_name]=doe"
来源:curl命令行
然后,在您的控制器中:
private def person_params
params.require(:person).permit(:first_name, :last_name)
end
并调用person_params
,而不是params
在Person.create...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句