Using ";action" from ActiveResource client

I have a RESTful Rails (1.2.3) app which implements XML operations. I’m
now
writing a client side (non-Rails) app to interact with it. The client
side
has activeresource and activesupport, both taken from SVN trunk.

I have basic ActiveResource REST fetches working OK:

unit = Unit.find(1)     # GET /units/1.xml
unit = Unit.find(:all, :params => { :foo => "bar" })  # GET 

/units.xml?foo=bar

Now, my problem is how to invoke named controller actions such as ;baz

unit = Unit.find(1, ??WHAT GOES HERE??)   # GET /units/1.xml;baz

On the Rails server side, the corresponding route is

map.resources :units, :member => {
:baz => :get,
}

and this part is working fine, e.g. I can point a browser to
http://localhost:8000/units/1.xml;baz
and get the expected result. So my only problem is how to get
ActiveResource
to issue this query in the first place.

I have tried

Unit.find(1, :from => "/units/1.xml;baz")

but tcpdump shows that it is only sending “GET /units/1.xml”.

So any ideas how to do this?

Alternatively: I could change my Rails application to accept
/units/1/baz.xml as the request format. I think this ought to be
possible,
as implied by the following example in the ActiveResource source code:

  #   Person.find(:one, :from => "/companies/1/manager.xml") # => 

GET /companies/1/manager.xml

However, how would I configure routing in the Rails app to accept a
request
in this format? At the moment, a request to /units/1/baz gives a 404.

I have the Agile book v2, and it shows how to do nested routes, but IIUC
that would be
/companies/1/managers
/companies/1/managers/1
and not
/companies/1/manager

Perhaps there is a way to declare a single resource rather than a
collection
in a mapping?

In any case, if I have to change my app to work this way, doesn’t that
make
the “;xxx” member actions superfluous?

Thanks,

Brian.

OK, I solved the first part of this question. It works if I do
find(:all) not find(1), i.e.

Unit.find(:all, :from => "/units/1.xml;baz")

It wasn’t exactly that simple. The ;baz controller actually returns a
collection of a different object type: …</
setting>. This confuses ARes, which was expecting
…. So actually what I have to write is

Setting.find(:all, :from => "/units/1.xml;baz")

But I can cope with that.

I’d still be interested in an answer to the other part of my question
though, which is not to do with ARes but Rails. If I want a RESTful
controller to respond to /things/1/foo[.xml], how do I configure the
routing to do that?

The ARes docs and unit tests imply that this is an expected scenario:


david = Person.find(:one, :from => “/companies/1/manager.xml”)
assert_equal “David”, david.name

Thanks,

Brian.

FYI, after digging around in subversion, it seems that in Edge Rails
the format of REST URLs has changed, from /foo/1.xml;action to /foo/1/
action.xml

Also, this new format is supported by ActiveResource in lib/
active_resource/custom_methods.rb, so you can issue queries like

settings = Unit.new(:id => '1').get(:baz)

(although in this case I get back a straight array of hashes, not
Setting objects). So now I need to decide whether to live life on The
Edge :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs