Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing ActiveResource POST to create an entry in another Rails application
    primarykey
    data
    text
    <p>There are 2 apps App1 and App2 and I am trying to create an entry into the <code>items</code> table of App2 from App1 using <code>ActiveResource</code></p> <p>I want this to work:</p> <pre><code>new_item = App2::Item.create(:property1 =&gt; "foo", :property2 =&gt; "bar") </code></pre> <p>This is what I am doing:</p> <p>In App1:</p> <pre><code>module App2 class Item &lt; 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 =&gt; params[:property1], :property2 =&gt; params[:property2] ) if @item.save format.xml { render :xml =&gt; @item, :status =&gt; :created, :location =&gt; @item } else format.xml { render :xml =&gt; @item.errors, :status =&gt; :unprocessable_entity} end rescue ActiveResource::ResourceNotFound =&gt; ex puts ex.message end end end end </code></pre> <p>In App2, CONTROLLER:</p> <pre><code> module App2 class ItemController &lt; ApplicationController def create_item begin @item = Item.new(:property1 =&gt; "foo", :property2 =&gt; "bar") @item.save respond_to do |format| if @item.save format.xml { render :xml =&gt; %Q[ &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;item&gt; &lt;property1&gt;#{item.property1}&lt;/property1&gt; &lt;property2&gt;#{item.property2}&lt;/property2&gt; &lt;/item&gt; ], :status =&gt; 200 } else format.xml { render :xml =&gt; %Q[&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;error&gt;Item was not created&lt;/error&gt;], :status =&gt; 404 } end end rescue Exception =&gt; ex format.xml { render :xml =&gt; %Q[&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;error&gt;Item was not created. REASON: #{ex.message}&lt;/error&gt;], :status =&gt; 404 } end end end end </code></pre> <p>App2, ROUTE:</p> <pre><code>map.connect 'api/create_item', :controller =&gt; 'app2/item', :action =&gt; "create_item", :conditions =&gt; {:method =&gt; :post} </code></pre> <p>When I am commenting the <code>self.prefix = "api/create_item/"</code>, I see some action in the App2 tail:</p> <pre><code>Processing ApplicationController#routes_catchall (for 10.104.232.160 at 2010-06-22 13:07:01) [POST] Parameters: {"item"=&gt;{"property1"=&gt; "abc", "property2"=&gt;"def"}, "action"=&gt;"routes_catchall", "path"=&gt;["items.xml"], "controller"=&gt;"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] </code></pre> <p>and a <code>Failed with 404 Not Found</code> in App1</p> <p>I see NO action there when I use it with the prefix.</p> <p>What am I messing up ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload