我正在尝试将表单数据从 Titanium 移动应用程序提交到 Ruby on Rails Web 应用程序。它是一个包含基本数据和图像的 POST 多部分请求。
我将展示我尝试提交数据的不同方式,以及我从 RoR 日志中得到的错误。
一些细节:
- 图片
problem[avatar]
来自手机摄像头。 - 以下代码对于下面的所有片段都是通用的。它出现在片段之前。
通用代码
var url = "http://some_RoR_app.herokuapp.com/problems";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload : function(e) {
alert('success');
},
// function called when an error occurs, including a timeout
onerror : function(e) {
alert('error');
},
timeout : 60 * 1000
});
方式#1
钛
我已将表单数据保存在变量中,然后将该变量传递给send()
函数。
var params = {
'problem[avatar]': $.report_image_view.reportPhoto.toImage(), // returns a [Ti.Blob] object
'problem[title]': $.report_title_view.title.value,
'problem[description]': $.report_description_view.description.value,
'problem[ptype]': $.report_type_view.report_type.value['valueID'],
'problem[status]': 1,
'problem[priority]': 2,
'problem[latitude]': latitude,
'problem[longitude]': longitude,
'problem[user_id]': 2
}
// Prepare the connection.
client.open("POST", url);
client.send(params);
RoR 日志
013-09-04T18:30:54.048412+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 18:30:54 +0000
2013-09-04T18:30:54.248656+00:00 app[web.1]: Processing by ProblemsController#create as MULTIPART_FORM
2013-09-04T18:30:54.248656+00:00 app[web.1]: [paperclip] Saving attachments.
2013-09-04T18:30:54.248656+00:00 app[web.1]: Redirected to https://km7.herokuapp.com/problems/32
2013-09-04T18:30:54.248656+00:00 app[web.1]: Completed 302 Found in 194ms (ActiveRecord: 80.2ms)
2013-09-04T18:30:54.252433+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=208ms status=302 bytes=103
2013-09-04T18:30:55.157538+00:00 app[web.1]: Started POST "/problems/32" for 111.222.333.444 at 2013-09-04 18:30:55 +0000
结果
帖子已完成,但未达到预期。一条新记录被添加到数据库中,但它是空的。换句话说,图片没有上传,标题是空的,描述、坐标和其他所有内容也是如此。仅设置时间戳和(对象)ID。
方式#2
钛
我所做的是,我没有将表单数据保存在变量中并传递该变量,而是使用表单数据作为传递给函数的参数创建了对象。
client.open("POST", url);
client.send({
"problem[avatar]" : $.report_image_view.reportPhoto.toImage(), // returns a [Ti.Blob] object
"problem[title]" : $.report_title_view.title.value,
"problem[description]" : $.report_description_view.description.value,
"problem[ptype]" : $.report_type_view.report_type["valueId"],
"problem[status]" : 1,
"problem[priority]" : 2,
"problem[latitude]" : latitude,
"problem[longitude]" : longitude,
"problem[user_id]" : 2
});
RoR 日志
2013-09-04T18:48:48.328758+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 18:48:48 +0000
2013-09-04T18:48:48.635395+00:00 app[web.1]: Processing by ProblemsController#create as MULTIPART_FORM
2013-09-04T18:48:48.635395+00:00 app[web.1]: [paperclip] Saving attachments.
2013-09-04T18:48:48.635395+00:00 app[web.1]: Redirected to https://km7.herokuapp.com/problems/34
2013-09-04T18:48:48.635395+00:00 app[web.1]: Completed 302 Found in 299ms (ActiveRecord: 66.1ms)
2013-09-04T18:48:48.644302+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=325ms status=302 bytes=103
结果
和以前一模一样。帖子已完成,但未达到预期。一条新记录被添加到数据库中,但它是空的。
方式#3
钛
这次我传递了一个带有参数的字符串,就好像它是一个 GET 一样。但是,我不能以这种方式传递图像......或者我可以吗?
var post_data = "problem[title]=Report rest client&problem[latitude]=18.09&problem[longitude]=-67.12&problem[ptype]=1&problem[status]=1&problem[priority]=2&problem[description]=Test description from rest client&problem[user_id]=2&problem[address]=123 Main Street, PR";
// Prepare the connection.
client.open("POST", url);
client.send(post_data);
RoR 日志
我不知道为什么,但它不断将我重定向到 /problems 直到它放弃。
2013-09-04T19:03:30.982008+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=10ms status=307 bytes=0
2013-09-04T19:03:36.415999+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:03:36 +0000
2013-09-04T19:03:36.475568+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=1ms service=64ms status=302 bytes=100
2013-09-04T19:03:36.476626+00:00 app[web.1]: Processing by ProblemsController#create as JS
2013-09-04T19:03:36.476626+00:00 app[web.1]: Parameters: {"problem"=>{"title"=>"Report rest client", "latitude"=>"18.09", "longitude"=>"-67.12", "ptype"=>"1", "status"=>"1", "priority"=>"2", "description"=>"Test description from rest client", "user_id"=>"2", "address"=>"Calle 3, Bo. Coto Sur"}}
2013-09-04T19:03:36.476626+00:00 app[web.1]: Redirected to https://some_RoR_app.herokuapp.com/problems
2013-09-04T19:03:36.476626+00:00 app[web.1]: Completed 302 Found in 54ms (ActiveRecord: 26.1ms)
2013-09-04T19:03:37.428602+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:03:37 +0000
2013-09-04T19:03:37.532547+00:00 app[web.1]: Processing by ProblemsController#create as JS
2013-09-04T19:03:37.532547+00:00 app[web.1]: Parameters: {"problem"=>{"title"=>"Report rest client", "latitude"=>"18.09", "longitude"=>"-67.12", "ptype"=>"1", "status"=>"1", "priority"=>"2", "description"=>"Test description from rest client", "user_id"=>"2", "address"=>"Calle 3, Bo. Coto Sur"}}
2013-09-04T19:03:37.532547+00:00 app[web.1]: Redirected to https://some_RoR_app.herokuapp.com/problems
2013-09-04T19:03:37.532547+00:00 app[web.1]: Completed 302 Found in 92ms (ActiveRecord: 61.0ms)
2013-09-04T19:03:38.457108+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=84ms status=302 bytes=100
2013-09-04T19:03:38.378766+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:03:38 +0000
2013-09-04T19:03:38.455542+00:00 app[web.1]: Processing by ProblemsController#create as JS
2013-09-04T19:03:38.455542+00:00 app[web.1]: Parameters: {"problem"=>{"title"=>"Report rest client", "latitude"=>"18.09", "longitude"=>"-67.12", "ptype"=>"1", "status"=>"1", "priority"=>"2", "description"=>"Test description from rest client", "user_id"=>"2", "address"=>"Calle 3, Bo. Coto Sur"}}
2013-09-04T19:03:38.455542+00:00 app[web.1]: Redirected to https://some_RoR_app.herokuapp.com/problems
2013-09-04T19:03:38.455542+00:00 app[web.1]: Completed 302 Found in 67ms (ActiveRecord: 48.9ms)
2013-09-04T19:03:39.319540+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:03:39 +0000
2013-09-04T19:03:39.361228+00:00 app[web.1]: Processing by ProblemsController#create as JS
2013-09-04T19:03:39.361228+00:00 app[web.1]: Parameters: {"problem"=>{"title"=>"Report rest client", "latitude"=>"18.09", "longitude"=>"-67.12", "ptype"=>"1", "status"=>"1", "priority"=>"2", "description"=>"Test description from rest client", "user_id"=>"2", "address"=>"Calle 3, Bo. Coto Sur"}}
2013-09-04T19:03:39.361228+00:00 app[web.1]: Redirected to https://some_RoR_app.herokuapp.com/problems
2013-09-04T19:03:39.361228+00:00 app[web.1]: Completed 302 Found in 38ms (ActiveRecord: 22.6ms)
2013-09-04T19:03:39.630525+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:03:39 +0000
2013-09-04T19:03:39.731993+00:00 app[web.1]: Processing by ProblemsController#create as JS
2013-09-04T19:03:39.731993+00:00 app[web.1]: Parameters: {"problem"=>{"title"=>"Report rest client", "latitude"=>"18.09", "longitude"=>"-67.12", "ptype"=>"1", "status"=>"1", "priority"=>"2", "description"=>"Test description from rest client", "user_id"=>"2", "address"=>"Calle 3, Bo. Coto Sur"}}
2013-09-04T19:03:39.731993+00:00 app[web.1]: Redirected to https://some_RoR_app.herokuapp.com/problems
2013-09-04T19:03:39.731993+00:00 app[web.1]: Completed 302 Found in 84ms (ActiveRecord: 37.1ms)
2013-09-04T19:03:39.741297+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=117ms status=302 bytes=100
结果
我没有看到数据库中有任何变化。什么都没有保存。
方式#4
钛
这次我以不同的方式组织 params 对象。否则,类似于方式#1
var params =
{ problem : {
avatar : $.report_image_view.reportPhoto.toImage(),
title : $.report_title_view.title.value,
description : $.report_description_view.description.value,
ptype : $.report_type_view.report_type.value['valueID'],
status : 1,
priority : 2,
latitude : latitude,
longitude : longitude,
user_id : 2
}}
// Prepare the connection.
client.open("POST", url);
client.send(params);
RoR 日志
2013-09-04T19:19:38.381670+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=4ms service=11ms status=307 bytes=0
2013-09-04T19:19:41.117687+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:19:41 +0000
2013-09-04T19:19:41.162099+00:00 app[web.1]: NoMethodError (undefined method `stringify_keys' for #<String:0x00000003672638>):
2013-09-04T19:19:41.162099+00:00 app[web.1]: app/controllers/problems_controller.rb:29:in `new'
2013-09-04T19:19:41.162099+00:00 app[web.1]: app/controllers/problems_controller.rb:29:in `create'
2013-09-04T19:19:41.162099+00:00 app[web.1]:
2013-09-04T19:19:41.162099+00:00 app[web.1]:
2013-09-04T19:19:41.162726+00:00 app[web.1]: Processing by ProblemsController#create as URL_ENCODED_FORM
2013-09-04T19:19:41.162726+00:00 app[web.1]: Parameters: {"problem"=>"{\n avatar = \"[object TiBlob]\";\n description = \"Enter a description\";\n latitude = \"37.33159255981445\";\n longitude = \"-122.0305099487305\";\n priority = 2;\n status = 1;\n title = \"A test title\";\n \"user_id\" = 2;\n}"}
2013-09-04T19:19:41.162726+00:00 app[web.1]: Completed 500 Internal Server Error in 39ms
2013-09-04T19:19:41.164682+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=50ms status=500 bytes=643
结果
数据库中没有保存任何内容。但是,这一次它显式地产生了问题控制器NoMethodError
的new
和create
方法。
方式#5
钛
这一次,我像Way #4一样创建对象,但将它send()
作为参数直接传递给方法,就像Way #3一样。
// Prepare the connection.
client.open("POST", url);
client.send({
problem : {
avatar : $.report_image_view.reportPhoto.toImage(),
title : $.report_title_view.title.value,
description : $.report_description_view.description.value,
ptype : $.report_type_view.report_type.value['valueID'],
status : 1,
priority : 2,
latitude : latitude,
longitude : longitude,
user_id : 2
}});
RoR 日志
2013-09-04T20:16:36.865831+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=3ms status=307 bytes=0
2013-09-04T20:16:38.772048+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 20:16:38 +0000
2013-09-04T20:16:38.808102+00:00 app[web.1]: NoMethodError (undefined method `stringify_keys' for #<String:0x00000005d45f58>):
2013-09-04T20:16:38.808102+00:00 app[web.1]: app/controllers/problems_controller.rb:29:in `new'
2013-09-04T20:16:38.808102+00:00 app[web.1]: app/controllers/problems_controller.rb:29:in `create'
2013-09-04T20:16:38.808102+00:00 app[web.1]:
2013-09-04T20:16:38.808102+00:00 app[web.1]:
2013-09-04T20:16:38.808786+00:00 app[web.1]: Processing by ProblemsController#create as URL_ENCODED_FORM
2013-09-04T20:16:38.808786+00:00 app[web.1]: Parameters: {"problem"=>"{\n avatar = \"[object TiBlob]\";\n description = \"Fix it asap.\";\n latitude = \"37.33069610595703\";\n longitude = \"-122.0306701660156\";\n priority = 2;\n status = 1;\n title = \"A broken water pipe\";\n \"user_id\" = 2;\n}"}
2013-09-04T20:16:38.808786+00:00 app[web.1]: Completed 500 Internal Server Error in 30ms
2013-09-04T20:16:38.809384+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=41ms status=500 bytes=643
结果
与方式 #4相同的错误
这一直让我发疯。
你知道我做错了什么吗?我怎样才能解决这个问题?我很乐意回答任何可以帮助您的问题。只需发表评论。