【发布时间】:2010-10-25 03:02:54
【问题描述】:
如何将 JSON 传递给 RAILS 应用程序,以便它在 has_many 关系中创建嵌套的子对象?
这是我目前所拥有的:
两个模型对象。
class Commute < ActiveRecord::Base
has_many :locations
accepts_nested_attributes_for :locations, :allow_destroy => true
end
class Location < ActiveRecord::Base
belongs_to :commute
end
通过 Commute,我设置了一个标准控制器。我希望能够使用 JSON 在单个 REST 调用中创建一个 Commute 对象以及几个子 Location 对象。我一直在尝试这样的事情:
curl -H "Content-Type:application/json" -H "Accept:application/json"
-d "{\"commute\":{\"minutes\":0,
\"startTime\":\"Wed May 06 22:14:12 EDT 2009\",
\"locations\":[{\"latitude\":\"40.4220061\",
\"longitude\":\"40.4220061\"}]}}" http://localhost:3000/commutes
或者更具可读性,JSON 是:
{
"commute": {
"minutes": 0,
"startTime": "Wed May 06 22:14:12 EDT 2009",
"locations": [
{
"latitude": "40.4220061",
"longitude": "40.4220061"
}
]
}
}
当我执行它时,我得到这个输出:
Processing CommutesController#create (for 127.0.0.1 at 2009-05-10 09:48:04) [POST]
Parameters: {"commute"=>{"minutes"=>0, "locations"=>[{"latitude"=>"40.4220061", "longitude"=>"40.4220061"}], "startTime"=>"Wed May 06 22:14:12 EDT 2009"}}
ActiveRecord::AssociationTypeMismatch (Location(#19300550) expected, got HashWithIndifferentAccess(#2654720)):
app/controllers/commutes_controller.rb:46:in `new'
app/controllers/commutes_controller.rb:46:in `create'
看起来 JSON 数组正在读取位置,但未解释为位置对象。
我可以轻松更改客户端或服务器,因此解决方案可以来自任何一方。
那么,RAILS 是否能让我轻松做到这一点?还是我需要在我的 Commute 对象中添加一些对此的支持?也许添加一个 from_json 方法?
感谢您的帮助。
正如我一直在解决这个问题,一种可行的解决方案是修改我的控制器。但这似乎不是“rails”的做法,所以如果有更好的方法,请告诉我。
def create
locations = params[:commute].delete("locations");
@commute = Commute.new(params[:commute])
result = @commute.save
if locations
locations.each do |location|
@commute.locations.create(location)
end
end
respond_to do |format|
if result
flash[:notice] = 'Commute was successfully created.'
format.html { redirect_to(@commute) }
format.xml { render :xml => @commute, :status => :created, :location => @commute }
else
format.html { render :action => "new" }
format.xml { render :xml => @commute.errors, :status => :unprocessable_entity }
end
end
end
【问题讨论】:
标签: ruby-on-rails ruby json