我的目標是通過一個REST請求創建嵌套的資源。 REST請求通過XML文檔表示。這對單個資源來說工作正常,但我無法管理它嵌套的資源。好的,接下來我會給你一個小例子。如何在XML中使用嵌套資源來使用REST?
首先創建一個新的Rails項目
rails forrest
接下來我們產生兩個資源,樹木和燕窩的支架。
./script/generate scaffold tree name:string
./script/generate scaffold bird_nest tree_id:integer bird_type:string eggs_count:integer
在./forrest/app/models/tree.rb我們下面插入,因爲一棵樹可以有很多鳥的巢「的has_many」行的文件:-)
class Tree < ActiveRecord::Base
has_many :bird_nests
end
在文件./forrest/app/models/bird_nest.rb我們在下面插入「belongs_to」這一行,因爲每個鳥巢都應該屬於一棵樹。
class BirdNest < ActiveRecord::Base
belongs_to :tree
end
後來我們建立數據庫並啓動服務器:
rake db:create
rake db:migrate
./script/server
只需複製並粘貼此XML sniplet到一個名爲 「tree.xml」 文件...
<tree>
<name>Apple</name>
</tree>
...並通過cURL將其發佈到服務以創建新樹:
curl -H 'Content-type: application/xml' -H 'Accept: application/xml' -d @tree.xml http://localhost:3000/trees/ -X POST
這工作正常。另外爲鳥巢XML(文件名「bird-nest.xml」)分開。如果我們發送此...
<bird-nest>
<tree-id>1</tree-id>
<bird-type>Sparrow</bird-type>
<eggs-count>2</eggs-count>
</bird-nest>
...也通過以下cURL語句。該資源已正確創建!
curl -H 'Content-type: application/xml' -H 'Accept: application/xml' -d @bird-nest.xml http://localhost:3000/bird_nests/ -X POST
確定一切都很好。現在來到橡膠與路面相遇的地方。我們在一個請求中創建兩個資源。因此,這裏是包含一個鳥巢爲我們的樹中的XML:
<tree>
<name>Cherry</name>
<bird-nests>
<bird-nest>
<bird-type>Blackbird</bird-type>
<eggs-count>2</eggs-count>
</bird-nest>
</bird-nests>
</tree>
我們觸發相應的請求通過捲曲再次使用...
curl -H 'Content-type: application/xml' -H 'Accept: application/xml' -d @tree-and-bird_nest.xml http://localhost:3000/trees/ -X POST
...現在我們會得到一個在(生成)「創造」樹的控制器的方法,服務器錯誤:AssociationTypeMismatch(燕窩預期,得到了陣列)
在我的角度來看,這是服務器的日誌中關於接收的屬性和錯誤MESSA的重要組成部分ge:
Processing TreesController#create (for 127.0.0.1 at 2009-02-17 11:29:20) [POST]
Session ID: 8373b8df7629332d4e251a18e844c7f9
Parameters: {"action"=>"create", "controller"=>"trees", "tree"=>{"name"=>"Cherry", "bird_nests"=>{"bird_nest"=>{"bird_type"=>"Blackbird", "eggs_count"=>"2"}}}}
SQL (0.000082) SET NAMES 'utf8'
SQL (0.000051) SET SQL_AUTO_IS_NULL=0
Tree Columns (0.000544) SHOW FIELDS FROM `trees`
ActiveRecord::AssociationTypeMismatch (BirdNest expected, got Array):
/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:150:in `raise_on_type_mismatch'
/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:146:in `replace'
/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:146:in `each'
/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:146:in `replace'
/vendor/rails/activerecord/lib/active_record/associations.rb:1048:in `bird_nests='
/vendor/rails/activerecord/lib/active_record/base.rb:2117:in `send'
/vendor/rails/activerecord/lib/active_record/base.rb:2117:in `attributes='
/vendor/rails/activerecord/lib/active_record/base.rb:2116:in `each'
/vendor/rails/activerecord/lib/active_record/base.rb:2116:in `attributes='
/vendor/rails/activerecord/lib/active_record/base.rb:1926:in `initialize'
/app/controllers/trees_controller.rb:43:in `new'
/app/controllers/trees_controller.rb:43:in `create'
所以我的問題是我做錯了關於嵌套的XML資源。這將是正確的XML語法?或者是否必須手動修改樹的控制器,因爲這種情況不在生成的範圍內?
看起來您可能需要編輯TreesController創建操作來處理創建BirdsNest對象。您可以發佈TreesController創建操作的副本嗎? – hernan43 2009-02-17 12:45:35
創建操作是之前步驟中生成的默認操作(請參閱「生成腳手架」)。 – 2009-02-17 14:04:47