1

有 2 个应用程序 App1 和 App2,我正在尝试items使用 App1 从 App2 表中创建一个条目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 中,控制器:

    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,路线:

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]

Failed with 404 Not FoundApp1 中的

当我将它与前缀一起使用时,我没有看到任何动作。

我在搞砸什么?

4

1 回答 1

0

好吧,我刚刚发现了问题:)
问题在于没有能力响应xml格式的路由。所以我不得不改变路线

map.connect 'api/create_item/items.:format', :controller => 'app2/item', :action => "create_item", :conditions => {:method => :post}

(实际上是我的一个朋友)connection.rb通过在文件中记录它正在命中的确切 url 来调试它。

希望这可以帮助某人:)

于 2010-06-22T10:10:56.113 回答