1
有2個應用程序App1和App2,我試圖用ActiveResource
使用的ActiveResource POST來創建另一個Rails應用程序
我想這個工作創造一個進入從應用1的items
表應用2的條目:
new_item = App2::Item.create(:property1 => "foo", :property2 => "bar")
這是我在做什麼:
在應用1:
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
在應用2,控制器:
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
應用2,路線:
map.connect 'api/create_item', :controller => 'app2/item', :action => "create_item", :conditions => {:method => :post}
當我評論的self.prefix = "api/create_item/"
,我看到在應用2尾一些行動:
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]
和Failed with 404 Not Found
in App1
I see NO ac當我用它的前綴時它在那裏。
我在搞什麼?
不錯..調試和記錄同時?? – bragboy 2010-06-22 10:12:27
Lol ...在「發現問題」中進行調試:P – Garfield 2010-06-22 10:15:36