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.
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: <settings><setting>...</
setting></settings>. This confuses ARes, which was expecting
<units><unit>...</unit></units>. 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.
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 :-)