2008年3月9日日曜日

'JSON Request/RESTを処理するサーバを作る'をやり直す.

追記
"' JSON Request/RESTを処理するサーバを作る'をやり直す"をやり直す というのを書いた.
createやupdate系はというかおそらくこれが正しい方法になる.
http://edotprintstacktrace.blogspot.com/2008/03/json-requestrest_09.html



先のエントリーの '1. JSON Request/RESTを処理するサーバを作る'だが'script/generate scaffold model ...'という手がある事を知り修正してみる (Rails 2.0.2).


1.1 'script/generate scaffold'で一気にモデル,コントローラ,ビュー(html用)を作る.
script/generate scaffold Project id:primary_key name:string

こいつでmodelもcontrollerもview(html用)もできてる.

1.2デモ用データの追加
script/generate migration InsertProjects

db/migrate/002_insert_projects.rb
class InsertProjects < ActiveRecord::Migration
def self.up
Project.create(:id=>1, :name=>"Foo")
Project.create(:id=>2, :name=>"Bar")

end

def self.down
end
end

反映
rake db:migrate


1.3 コントローラにjson部分を追加する
show以外はよくわからんがxml用のヤツをcopyしてjsonとしてみた…(教えて優しい人).
(json用もできるオプションとかあるのか?)
app/controlers/projects_controller.rb
class ProjectsController < ApplicationController
# GET /projects
# GET /projects.xml
def index
@projects = Project.find(:all)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @projects }
format.json { render :json=> @projects }
end
end

# GET /projects/1
# GET /projects/1.xml
def show
@project = Project.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @project }
format.json { render :json=> @project }
end
end

# GET /projects/new
# GET /projects/new.xml
def new
@project = Project.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @project }
format.json { render :json=> @project }
end
end

# GET /projects/1/edit
def edit
@project = Project.find(params[:id])
end

# POST /projects
# POST /projects.xml
def create
@project = Project.new(params[:project])

respond_to do |format|
if @project.save
flash[:notice] = 'Project was successfully created.'
format.html { redirect_to(@project) }
format.xml { render :xml => @project, :status => :created, :location => @project }
format.json { render :json=> @project, :status => :created, :location => @project }
else
format.html { render :action => "new" }
format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
format.json { render :json=> @project.errors, :status => :unprocessable_entity }
end
end
end

# PUT /projects/1
# PUT /projects/1.xml
def update
@project = Project.find(params[:id])

respond_to do |format|
if @project.update_attributes(params[:project])
flash[:notice] = 'Project was successfully updated.'
format.html { redirect_to(@project) }
format.xml { head :ok }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
format.json { render :json=> @project.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /projects/1
# DELETE /projects/1.xml
def destroy
@project = Project.find(params[:id])
@project.destroy

respond_to do |format|
format.html { redirect_to(projects_url) }
format.xml { head :ok }
format.json { head :ok }
end
end
end

とりあえずこんな感じ?

1.x 試す
curl -H 'Accept: application/json' http://localhost:3000/projects/1
{"name": "Foo", "updated_at": "2008/03/09 00:00:00 +0900", "id": 1, "created_at": "2008/03/09 00:00:00 +0900"}


curl -H 'Accept: application/json' http://localhost:3000/projects
[{"name": "Foo", "updated_at": "2008/03/09 00:00:00 +0900", "id": 1, "created_at": "2008/03/09 00:00:00 +0900"}, {"name": "Bar", "updated_at": "2008/03/09 00:00:00 +0900", "id": 2, "created_at": "2008/03/09 00:00:00 +0900"}]

0 件のコメント: