# REST own action

**URL:** <https://www.ruby-forum.com/t/rest-own-action/103812>\
**Category:** Rails\
**Created:** [May 10, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812 "2007-05-10T15:54:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/1 "2007-05-10T15:54:43Z")

</div>

I can sent my own action to a users\_controller stating in routes.rb  
map.resources :users, :member =\> { :network =\> get }

I defined in my users\_controller the network action…  
so I can have an url like  
“[http://localhost:3000/users/168.xml;network](http://localhost:3000/users/168.xml;network)”

now I would like to have another action network\_plus which is a little  
bit different fron the network…

sure, I can state another action… but is there any possibility to  
use only one action and perform differently according to a parameter…

how can I pass parameters to the network action ?  
“[http://localhost:3000/users/168.xml;network&plus=true](http://localhost:3000/users/168.xml;network&plus=true)” … is it  
correctly RESTFUL?..

thanks for your lights

kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/2 "2007-05-10T16:04:20Z")

</div>

On 5/10/07, Kad K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> sure, I can state another action… but is there any possibility to  
> use only one action and perform differently according to a parameter…
> 
> how can I pass parameters to the network action ?  
> “[http://localhost:3000/users/168.xml;network&plus=true](http://localhost:3000/users/168.xml;network&plus=true)” … is it  
> correctly RESTFUL?..
> 
> thanks for your lights

Parameters need to start with a ?, and are separated by the &.

…/168.xml;network?plus=true

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/3 "2007-05-10T16:10:36Z")

</div>

Rick O. wrote:

> On 5/10/07, Kad K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > sure, I can state another action… but is there any possibility to  
> > use only one action and perform differently according to a parameter…
> > 
> > how can I pass parameters to the network action ?  
> > “[http://localhost:3000/users/168.xml;network&plus=true](http://localhost:3000/users/168.xml;network&plus=true)” … is it  
> > correctly RESTFUL?..
> > 
> > thanks for your lights
> 
> Parameters need to start with a ?, and are separated by the &.
> 
> …/168.xml;network?plus=true
> 
> –  
> Rick O.  
> [http://lighthouseapp.com](http://lighthouseapp.com)  
> [http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
> [http://mephistoblog.com](http://mephistoblog.com)

thanks sorry for the typing mistake… so it’s so simple as url…  
thanks

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/4 "2007-05-10T16:12:21Z")

</div>

On May 10, 8:54 am, Kad K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> sure, I can state another action… but is there any possibility to  
> use only one action and perform differently according to a parameter…
> 
> how can I pass parameters to the network action ?  
> “[http://localhost:3000/users/168.xml;network&plus=true](http://localhost:3000/users/168.xml;network&plus=true)” … is it  
> correctly RESTFUL?..

It’s ok to use the parameters to further “refine” your action, but  
just make sure that your custom action shouldn’t really be a standard  
action of another resource. It could very well be that although your  
“network” action seems to be needed on a user resource, perhaps you’re  
really stumbling upon a discovery of a new resource entirely.

What does the network action try to do? And if you _had_ to stick to  
the seven RESTful actions, what action would “network” be and for what  
resource?

Those are the kinds of questions I ask myself whenever I feel like  
adding a custom action, and about half the time, I’m able to just  
declare a new resource and remove the custom action.

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/5 "2007-05-10T16:21:24Z")

</div>

On Thu, May 10, 2007 at 02:11:15PM -0000, Jeff wrote :

> Those are the kinds of questions I ask myself whenever I feel like  
> adding a custom action, and about half the time, I’m able to just  
> declare a new resource and remove the custom action.

+1.

Actually, everything (authorizations, logins, search, …) can be fit  
into  
the 7 REST actions. It’s just sometimes hard to figure out what sort of  
resource do you need.

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/6 "2007-05-10T17:16:37Z")

</div>

Is this URL correct?

…/168.xml;network

or should it actually be

…/168;network.xml

this always confuses me. Should the format extension always appear at  
very end of the URL, just before the query string. Before the  
semicolon before the custom action?

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/7 "2007-05-10T17:21:38Z")

</div>

On 5/10/07, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> very end of the URL, just before the query string. Before the  
> semicolon before the custom action?

I think this was one of the reasons we changed it to / in edge rails.  
When we actually got to building real restful apps with custom  
actions, singleton resources, etc, it was clear using the semicolon  
wasn’t working out (plus, safari won’t send http basic auth with it).  
It should be /168/network.xml, which is a lot easier to grasp,  
provides a transparent migration to a singleton ‘network’ resource  
should it be necessary, and doesn’t mess with caching.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/8 "2007-05-10T17:31:39Z")

</div>

Oh thank goodness. Great move! I’m looking forward to the release of  
this. I like that idea so much better.

It also removes the confusion between

[http://localhost:3000/resource/new](http://localhost:3000/resource/new)

and

[http://localhost:3000/resource/:id;edit](http://localhost:3000/resource/:id;edit)

If you think about it they are both used to GET the form and not  
modify the resource. Yet “new” has a slash and “edit” has a  
semicolon.

So I would love to write this instead:

[http://localhost:3000/resource/:id/edit](http://localhost:3000/resource/:id/edit)

And this will also make custom actions (when they are necessary) feel  
more like first class citizens in the URL.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/9 "2007-05-10T17:38:28Z")

</div>

Rick O. wrote:

> On 5/10/07, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > very end of the URL, just before the query string. Before the  
> > semicolon before the custom action?
> 
> I think this was one of the reasons we changed it to / in edge rails.  
> When we actually got to building real restful apps with custom  
> actions, singleton resources, etc, it was clear using the semicolon  
> wasn’t working out (plus, safari won’t send http basic auth with it).  
> It should be /168/network.xml, which is a lot easier to grasp,  
> provides a transparent migration to a singleton ‘network’ resource  
> should it be necessary, and doesn’t mess with caching.
> 
> –  
> Rick O.  
> [http://lighthouseapp.com](http://lighthouseapp.com)  
> [http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
> [http://mephistoblog.com](http://mephistoblog.com)

could be, but…  
no route found to match “/users/168/network.xml” with {:method=\>:get}  
no route found to match “/users/168;network.xml” with {:method=\>:get}

only [http://localhost:3000/users/168.xml;network](http://localhost:3000/users/168.xml;network) is correctly  
executed…

also how shoudl I write the URL in my rxml ?, I wrote  
:dataURL =\>  
“[http://localhost:3000/users/#{friend.id}.xml;network?from=#{@user.id}&level=#{@level}](http://localhost:3000/users/#%7Bfriend.id%7D.xml;network?from=#%7B@user.id%7D&level=#%7B@level%7D)”

which generates…  
dataURL=“[http://localhost:3000/users/126.xml;network?from=168&amp;level=1](http://localhost:3000/users/126.xml;network?from=168&amp;level=1)”  
this URL accepted but u can see the “amp;level” parameter ???  
params =\> {“format”=\>“xml”, “amp;level”=\>“1”, “from”=\>“168”, “amp”=\>“”,  
“level”=\>“1”, “action”=\>“network”, “id”=\>“126”, “controller”=\>“users”}

thanks for your help  
kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/10 "2007-05-10T17:26:39Z")

</div>

On Thu, May 10, 2007 at 08:15:31AM -0700, Robert W. wrote :

> Before the semicolon before the custom action?

Be careful, semicolon will be deprecated in Rails2.0  
[http://dev.rubyonrails.org/changeset/6485](http://dev.rubyonrails.org/changeset/6485)

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/11 "2007-05-10T18:07:01Z")

</div>

Jeff C. wrote:

> On May 10, 8:54 am, Kad K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > sure, I can state another action… but is there any possibility to  
> > use only one action and perform differently according to a parameter…
> > 
> > how can I pass parameters to the network action ?  
> > “[http://localhost:3000/users/168.xml;network&plus=true](http://localhost:3000/users/168.xml;network&plus=true)” … is it  
> > correctly RESTFUL?..
> 
> It’s ok to use the parameters to further “refine” your action, but  
> just make sure that your custom action shouldn’t really be a standard  
> action of another resource. It could very well be that although your  
> “network” action seems to be needed on a user resource, perhaps you’re  
> really stumbling upon a discovery of a new resource entirely.
> 
> What does the network action try to do? And if you _had_ to stick to  
> the seven RESTful actions, what action would “network” be and for what  
> resource?
> 
> Those are the kinds of questions I ask myself whenever I feel like  
> adding a custom action, and about half the time, I’m able to just  
> declare a new resource and remove the custom action.
> 
> Jeff  
> [softiesonrails.com](http://softiesonrails.com)

the network action return an xml files where the ‘friends’ of a user are  
described (friends\_who\_know\_me and friends\_i\_know) one node  
for each friend and one node for each friend.residence… it’s a  
complete description of a user’s network, which is built from user’s  
relationships  
so it’s a GET the network of a User…

I agree that it’s a resource but not in the DB… it’s point I did not  
catch very well in RESTFul approach, it seems that all resources ARE  
ActiveResources… true or false ?

kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/12 "2007-05-10T18:23:00Z")

</div>

On Thu, May 10, 2007 at 06:07:01PM +0200, Kad K. wrote :

> the network action return an xml files where the ‘friends’ of a user are  
> described (friends\_who\_know\_me and friends\_i\_know) one node  
> for each friend and one node for each friend.residence… it’s a  
> complete description of a user’s network, which is built from user’s  
> relationships  
> so it’s a GET the network of a User…

Why don’t you create a resource ‘network’ which returns your formatted  
xml? You could also add routes like ‘network/fred/paris’  
(‘network/:person/:location/’) to GET from  
this resource only the friends of fred located in Paris.

> I agree that it’s a resource but not in the DB… it’s point I did not  
> catch very well in RESTFul approach, it seems that all resources ARE  
> ActiveResources… true or false ?

Resources are only a way to see some data on your network. It’s like  
models: it could be directly linked to real data in your database or  
not. For example, I use an ‘auth’ resource for my login/logout actions  
which  
maps to cookies.  
login POST a cookie, is\_logged\_in? is a GET on a cookie etc.

My $0.02,

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [May 10, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/rest-own-action/103812/13 "2007-05-10T19:24:07Z")

</div>

> could be, but…  
> no route found to match “/users/168/network.xml” with {:method=\>:get}  
> no route found to match “/users/168;network.xml” with {:method=\>:get}
> 
> only [http://localhost:3000/users/168.xml;network](http://localhost:3000/users/168.xml;network) is correctly  
> executed…

The change was made in edge rails for Rails 2.0.

> also how shoudl I write the URL in my rxml ?, I wrote  
> :dataURL =\>  
> “[http://localhost:3000/users/#{friend.id}.xml;network?from=#{@user.id}&level=#{@level}](http://localhost:3000/users/#%7Bfriend.id%7D.xml;network?from=#%7B@user.id%7D&level=#%7B@level%7D)”

network\_user\_path(:id =\> friend, :from =\> @user, :level =\> @level)  
formatted\_network\_user\_path(:id =\> friend, :from =\> @user, :level =\>  
@level, :format =\> :xml)

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
