解决使用json进行POST的create(params [:subscription])

时间:2012-06-06 19:52:09

标签: ruby-on-rails ruby-on-rails-3

以下查询

$ curl http://localhost:3000/subscriptions.json -d campaign=CAMPAIGN -d phone=6446342542 -d auth_token=QWvHBzkzGCef58osasxz

正在返回

{"errors":{"phone":["can't be blank","Please enter a valid phone number."],"campaign":["can't be blank"]}}

行动是

def create
  @subscription = Subscription.create(params[:subscription])
  respond_with(@subscription)
end

错误似乎是因为我没有通过json发送params[:subscription]作为我之前的Implementing a specific action for a model that respond_with(@object)问题的回复。

如果我尝试添加Subscription.create(params[:subscription][:phone],..)等细节,我会

<h1>
 NoMethodError
   in SubscriptionsController#create

    

undefined method `[]' for nil:NilClass

1 个答案:

答案 0 :(得分:1)

curl -v \
-H "Accept: application/json" \
-H "Content-type: application/json" \
-X POST \
-d '{"subscription":{"campaign":"CAMPAIGN","phone":"6446342542"}, \
"auth_token":"QWvHBzkzGCef58osasxz"}' \
http://localhost:3000/subscriptions.json

尝试类似的东西(在一行中可能是优选的......没有反斜杠......我这样做是为了你的阅读清晰度......但它也适用于反斜杠)