有2个应用App1和App2,我正在尝试使用items
ActiveResource
表中的条目
我希望这项工作:
new_item = App2::Item.create(:property1 => "foo", :property2 => "bar")
这就是我在做的事情:
在App1中:
module App2
class Item < ActiveResource::Base
self.site = "http://localhost:3001" # This is where App2 is running
self.prefix = "api/create_item/"
def self.create(params)
begin
@item = App2::Item.new(:property1 => params[:property1], :property2 => params[:property2] )
if @item.save
format.xml { render :xml => @item, :status => :created, :location => @item }
else
format.xml { render :xml => @item.errors, :status => :unprocessable_entity}
end
rescue ActiveResource::ResourceNotFound => ex
puts ex.message
end
end
end
end
在App2中,CONTROLLER:
module App2
class ItemController < ApplicationController
def create_item
begin
@item = Item.new(:property1 => "foo", :property2 => "bar")
@item.save
respond_to do |format|
if @item.save
format.xml { render :xml => %Q[
<?xml version="1.0" encoding="UTF-8"?>
<item>
<property1>#{item.property1}</property1>
<property2>#{item.property2}</property2>
</item>
], :status => 200 }
else
format.xml { render :xml => %Q[<?xml version="1.0" encoding="UTF-8"?><error>Item was not created</error>], :status => 404 }
end
end
rescue Exception => ex
format.xml { render :xml => %Q[<?xml version="1.0" encoding="UTF-8"?><error>Item was not created. REASON: #{ex.message}</error>], :status => 404 }
end
end
end
end
App2,ROUTE:
map.connect 'api/create_item', :controller => 'app2/item', :action => "create_item", :conditions => {:method => :post}
当我评论self.prefix = "api/create_item/"
时,我在App2尾部看到了一些动作:
Processing ApplicationController#routes_catchall (for 10.104.232.160 at 2010-06-22 13:07:01) [POST]
Parameters: {"item"=>{"property1"=> "abc", "property2"=>"def"}, "action"=>"routes_catchall", "path"=>["items.xml"], "controller"=>"application"}
Rendering template within layouts/error
Rendering error/404 (404 Not Found)
Completed in 743ms (View: 726, DB: 1) | 404 Not Found [http://localhost:3001/items.xml]
和App1中的Failed with 404 Not Found
当我使用前缀时,我看不到任何动作。
我搞砸了什么?
答案 0 :(得分:0)
好吧,我刚发现问题:)
问题在于路由中没有能力响应xml格式。
所以我不得不改变路线
map.connect 'api/create_item/items.:format', :controller => 'app2/item', :action => "create_item", :conditions => {:method => :post}
我(实际上我的朋友)通过记录它在connection.rb
文件中点击的确切网址来调试它。
希望这有助于某人:)