# Using ";action" from ActiveResource client

**URL:** <https://www.ruby-forum.com/t/using-action-from-activeresource-client/103460>\
**Category:** Rails\
**Created:** [May 6, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/using-action-from-activeresource-client/103460 "2007-05-06T12:42:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [May 6, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/using-action-from-activeresource-client/103460/1 "2007-05-06T12:42:28Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [May 6, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/using-action-from-activeresource-client/103460/2 "2007-05-06T22:33:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [May 7, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/using-action-from-activeresource-client/103460/3 "2007-05-07T21:00:54Z")

</div>

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 🙂
