3
我有一些问题,发布JSON数据到我的轨道控制器,并从我的JSON数据获取字段映射到paremeters创建我的实体。将JSON数据发布到rails控制器?
这里的作用是什么样子:
def create
@trip = Trip.new(params[:trip])
if @trip.save
respond_to do |format|
format.json{render :json => @trip, :status => :created, :location => @trip }
end
end
end
下面是从web服务器的输出:
Started POST "/trips.json" for 127.0.0.1 at Sat Sep 03 12:37:41 -0400 2011
Processing by TripsController#create as JSON
Parameters: {"title"=>"Charlie"}
AREL (0.5ms) INSERT INTO "trips" ("created_at", "updated_at", "title") VALUES ('2011-09-03 16:37:41.945743', '2011-09-03 16:37:41.945743', NULL)
Completed 201 Created in 15ms (Views: 3.5ms | ActiveRecord: 0.5ms)
正如你可以看到它的插入NULL到标题字段,我期待它从JSON数据映射到它?
这里的HTTP请求:
Host localhost:3000
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:6.0.1) Gecko/20100101 Firefox/6.0.1
Accept */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
Content-Type application/json; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://localhost:3000/trips/new
Content-Length 19
Cookie _tripplanner_session=BAh7ByIQX2NzcmZfdG9rZW4iMWJCVDc0YWNIeHROcmw5VC9ZVEhMdFNlR0dtTWtSVytCL0dwTi9yam1CUUU9Ig9zZXNzaW9uX2lkIiU1NzhlMDAwNmM2N2RkMTE1YTA3M2ZmMzA4NDQ0M2NmOQ%3D%3D--1c2f1fc1d7c1bd147fa1cf9cff47e77c050f51be
Pragma no-cache
Cache-Control no-cache