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
結果
データベースには何も保存されませんでした。ただし、今回は Problems コントローラーのandメソッドで明示的に を作成しNoMethodError
ました。new
create
方法#5
チタン
今回は方法4のようにオブジェクトを作成しますが、方法3send()
のようにパラメーターとして直接メソッドに渡します。
// 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と同じエラー
これは私を夢中にさせています。
私が間違っていることを知っていますか?どうすればこれを修正できますか? ご不明な点がございましたら、お気軽にお問い合わせください。コメントを残してください。