5

I have route that looks like this:

/orders/preview

It returns something like:

<order><total>100</total></order>

I want to add this method to an active resource class. What is the best way to do this?

I started doing something like this:

class Order < ActiveResource::Base
  def self.preview(params = {})
    post(:preview, params)
  end
end

This appears to work, but I get a Net::HTTP response back rather than an Order object. What am I doing wrong?

makes
  • 6,438
  • 3
  • 40
  • 58

2 Answers2

0

I had a similar problem with different formats being processed... I have solved this by explicitly stating what formats I want to use like this:

in ActiveResource model, set self.format parameter

class Order < ActiveResource::Base
    self.site = "http://lbv.me"
    self.format = :json  
end

In ActiveRecord model, if you use 'respond_with' method you actually must state what format you expect it to respond with, like this:

class UsersController < ApplicationController
respond_to :html, :json

def show
   @user = User.find(params[:id])
   respond_with @user
end
.
.
.
end
Dmitry Matveev
  • 5,320
  • 1
  • 32
  • 43
0

I am not sure about POST, but if you just wanted to do a GET. Have you tried this?

http://api.rubyonrails.org/v3.2.1/classes/ActiveResource/Base.html#method-c-find

Order.find(:all, :from => :preview)
# => GET /orders/preview.json
dgilperez
  • 10,716
  • 8
  • 68
  • 96
vinhboy
  • 8,542
  • 7
  • 34
  • 44