Martin
Martin

Reputation: 11336

solving create(params[:subscription]) doing POST with json

The following query

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

is returning

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

The action is

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

The error seems to be because I'm not sending params[:subscription] through json as replied on my previous Implementing a specific action for a model that respond_with(@object) question.

If I try to add specifics such as Subscription.create(params[:subscription][:phone],..) I get

<h1>
 NoMethodError
   in SubscriptionsController#create

undefined method `[]' for nil:NilClass

Upvotes: 0

Views: 79

Answers (1)

p.matsinopoulos
p.matsinopoulos

Reputation: 7810

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

Try something like that (in one line may be preferrable...without the backslashes...I did that for your reading clarity....but it works with the backslashes too)

Upvotes: 1

Related Questions