views:

48

answers:

1

Hi

There are 2 apps App1 and App2 and I am trying to create an entry into the items table of App2 from App1 using ActiveResource

I want this to work:

new_item = App2::Item.create(:property1 => "foo", :property2 => "bar")

This is what I am doing:

In 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

In 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}  

When I am commenting the self.prefix = "api/create_item/", I see some action in the App2 tail:

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]

and a Failed with 404 Not Found in App1

I see NO action there when I use it with the prefix.

What am I messing up ?

A: 

Well, I just found the problem :)
The problem lied in the route which did not have the capability to respond to the xml format. so I had to change the route to

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

I (well actually a friend of mine) debugged it by logging the exact url it was hitting in connection.rb file.

Hope this helps someone :)

Shikher
Nice.. debugging and logging simultaneously ??
Bragboy
Lol... Debugging as in "spotting the problem" :P
Shikher