powerboy
powerboy

Reputation: 10961

Rails' wrap_parameters is not working

I have wrap_parameters format: [:json] in wrap_parameters.rb. I post to the server with jQuery.post('photos', {name: 'flower'});. In photos#create, params is

{"name"=>"flower", "controller"=>"photos", "action"=>"create"}

But I am expecting

{"name"=>"flower", "controller"=>"photos", "action"=>"create", "photos"=>{"name"=>"flower"}}

What am I missing?

Upvotes: 3

Views: 2106

Answers (2)

Hugo Bonnome
Hugo Bonnome

Reputation: 41

You have to perform your post request with the header "Content-Type": "application/json".

Upvotes: 0

dimuch
dimuch

Reputation: 12818

The jQuery.post('photos', {name: 'flower'}); call posts params as 'standard' form (application/x-www-form-urlencoded or multipart/form-data), not json. So wrap_parameters format: [:json] does not work.

Try to remove format: [:json] or post json data, something like

$.ajax({
  url:'photos',
  type:"POST",
  data:JSON.stringify({name: 'flower'}),
  contentType:"application/json; charset=utf-8",
  dataType:"json",
  success: function(){
    ...
  }
})

Upvotes: 6

Related Questions