开发者

Sench Touch (Extjs) not POSTing nested JSON to Rails 3 app correctly

开发者 https://www.devze.com 2023-02-04 15:32 出处:网络
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.

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消