phonegap から rails 3.2.2 scaffold への簡単な投稿をしようとしています。1つのコントローラー=>画像と1つのフィールド=> t.string:nameで構成される単純な足場があります。
class ImagesController < ApplicationController
# GET /images
# GET /images.json
def index
@images = Image.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @images }
end
end
# GET /images/1
# GET /images/1.json
def show
@image = Image.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @image }
end
end
# GET /images/new
# GET /images/new.json
def new
@image = Image.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @image }
end
end
# GET /images/1/edit
def edit
@image = Image.find(params[:id])
end
# POST /images
# POST /images.json
def create
@image = Image.new(params[:image])
respond_to do |format|
if @image.save
format.html { redirect_to @image, notice: 'Image was successfully created.' }
format.json { render json: @image, status: :created, location: @image }
else
format.html { render action: "new" }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
# PUT /images/1
# PUT /images/1.json
def update
@image = Image.find(params[:id])
respond_to do |format|
if @image.update_attributes(params[:image])
format.html { redirect_to @image, notice: 'Image was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
# DELETE /images/1
# DELETE /images/1.json
def destroy
@image = Image.find(params[:id])
@image.destroy
respond_to do |format|
format.html { redirect_to images_url }
format.json { head :no_content }
end
end
end
phonegap アプリの index.html ファイルは次のとおりです。
<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.6.1.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
function appReady(){
var ajax = new XMLHttpRequest();
var data = '{ image: { name: "noway" } }';
ajax.open("POST","http://<server>/images.json",true);
ajax.setRequestHeader("Content-type", "application/json")
ajax.send(data);
ajax.onreadystatechange=function(){
if(ajax.readyState==4 && (ajax.status==200)){
document.getElementById('main').innerHTML = ajax.responseText;
}
}
}
document.addEventListener("deviceready", appReady, false);
</script>
</head>
<body>
<h1>Hello World</h1>
<div id="main">
</div>
</body>
</html>
Webrick コンソールは以下を生成します。
Started POST "/images.json" for 10.0.1.14 at 2012-04-22 22:32:58 -0500
Error occurred while parsing request parameters.
Contents:
MultiJson::DecodeError (756: unexpected token at '{ image: { name: "noway" } }'):
json (1.6.6) lib/json/common.rb:148:in `parse'
変更すると次のようになるため、jsonオブジェクトをフォーマットする方法と関係があると思います。
var data = '{ image: { name: "noway" } }';
に:
var data = " ";
私は得る:
Started POST "/images.json" for 10.0.1.14 at 2012-04-22 22:57:03 -0500
Processing by ImagesController#create as JSON
Parameters: {"image"=>{}}
WARNING: Can't verify CSRF token authenticity
(0.1ms) begin transaction
SQL (24.1ms) INSERT INTO "images" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Mon, 23 Apr 2012 03:57:03 UTC +00:00], ["name", nil], ["updated_at", Mon, 23 Apr 2012 03:57:03 UTC +00:00]]
(46.0ms) commit transaction
Completed 201 Created in 76ms (Views: 1.9ms | ActiveRecord: 70.2ms)
どういうわけかjsonデータの形成が間違っていますか?