1

I am trying to create a form with Sencha Touch that will create a new Task in a simple Rails 3 application. I am essentially adding nested JSON to this question.

To make testing it easy I am hardcoding the params into the request. The Rails app was created using:

$rails g scaffold task name:string

Sencha Touch Ajax request:

Ext.Ajax.request({
 url:'/tasks',
 method:'POST',
 params: { 
  task: { name: "Hello World" }
  }

Rails expects the params hash to look like this:

Parameters: { "task"=>{"name"=>"Hello World"} }

But the Ajax POST from Sencha sends it like this:

Parameters: {"task"=>"[object Object]"}

When I try using defaultHeaders like:

Ext.Ajax.defaultHeaders = {
  'Content-Type': 'application/json'
}

It posts like this:

Parameters: {"_json"=>"task=%5Bobject%20Object%5D"}

Any thoughts on how to handle this properly?

Community
  • 1
  • 1
Andrew Gertig
  • 138
  • 1
  • 2
  • 7

2 Answers2

4

As per http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters, Rails creates 'nested' parameters when it sees form fields using [...] syntax.

Ext.Ajax.request({
    url:'/tasks',
    method:'POST',
    params: {
        'task[name]':'Hello World'
    }
});

I don't have a Rails server to test this out against, but the POST body looks about right.

James Pearce
  • 2,332
  • 15
  • 14
0

As an interim answer, did you consider using the Sencha Touch Ext.data.* package? In there, there's a REST proxy that will handle all the CRUD operations between your server and the client app, and take care of model field updates, validation, associations etc etc

That doesn't answer your exact question, which I'll look at now, but something to think about.

James Pearce
  • 2,332
  • 15
  • 14
  • Hey James, I tried using Ext.data.AjaxProxy for a bit but didn't have much luck as it seemed that almost every example on usage was a GET request rather than a POST. I did try to sort it out on my own but kept running into errors. I does seem as though AjaxProxy would be a good choice though if I could find better documentation/examples. – Andrew Gertig Jan 14 '11 at 22:34