# Non-creative POSTs with REST

**URL:** <https://www.ruby-forum.com/t/non-creative-posts-with-rest/83525>\
**Category:** Rails\
**Created:** [January 26, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/non-creative-posts-with-rest/83525 "2007-01-26T22:40:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ed\_Hickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/258eb7/32.png) [@Ed\_Hickey](https://www.ruby-forum.com/u/Ed_Hickey)\
**Post date:** [January 26, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/non-creative-posts-with-rest/83525/1 "2007-01-26T22:40:14Z")

</div>

I have a view with data that needs to be sorted and/or filtered.  
Previously, I had a form that would POST the filters or sorting commnds  
to  
the page. This breaks in REST because the auto-created routes tell  
rails I  
want to create a new record. For instance, map.resources :assets  
creates  
this route:

POST /assets/ {:controller=\>“assets”,  
:action=\>“create”}

but, of course, posting the filter-form to that location invokes the  
create  
action when I really just want to invoke the index action. I came up  
with  
this, but it seems a bit dodgy:

map.assets\_stats ‘/assets/stats’, :controller =\> “assets”, :action =\>  
“index”

If I have 10 controllers that all need this functionality, it becomes  
fairly  
un-DRY…  
Is the only solution to change my forms to GET instead of POST? Got to  
be a  
better way…

Thanks,  
Ed

---

<div class="post-metadata">

**Author:** ![Ed\_Hickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/258eb7/32.png) [@Ed\_Hickey](https://www.ruby-forum.com/u/Ed_Hickey)\
**Post date:** [January 26, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/non-creative-posts-with-rest/83525/2 "2007-01-26T22:45:15Z")

</div>

On 1/26/07, Ed Hickey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> this, but it seems a bit dodgy:  
> Thanks,  
> Ed
> 
> >

Ed,

Just add a sort action:

map.resources :assets, :collection =\> { :sort =\> :post }

Hope this helps.

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![Ed\_Hickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/258eb7/32.png) [@Ed\_Hickey](https://www.ruby-forum.com/u/Ed_Hickey)\
**Post date:** [January 26, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/non-creative-posts-with-rest/83525/3 "2007-01-26T22:51:16Z")

</div>

> map.resources :assets, :collection =\> { :sort =\> :post }

With a little creativity, or Jamis B.'s blog in your feed reader  
[1], you can bust off some with\_options action:

map.with\_options :collection =\> { :sort =\> :post } do |sortable\_map|  
sortable\_map.resources :assets  
sortable\_map.resources :articles  
end

Course, that may break down if some of your resources modify  
:collection on their own. Another option if you have a bunch of  
identical resources is to do this:

map.resources :assets, :articles, :collection =\> { :sort =\> :post }

1. [Buckblog: Object#with\_options](http://weblog.jamisbuck.org/2007/1/24/object-with_options)

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

---

<div class="post-metadata">

**Author:** ![Ed\_Hickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/258eb7/32.png) [@Ed\_Hickey](https://www.ruby-forum.com/u/Ed_Hickey)\
**Post date:** [January 26, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/non-creative-posts-with-rest/83525/4 "2007-01-26T23:05:18Z")

</div>

Should have mentioned that I did try that approach (using :collection)  
but  
this is what gave me:

POST /assets;sort/ {:controller=\>“assets”, :action=\>“sort”}

I want the action to be “index”, not “sort” because the data is all  
displayed in the index view. I want to POST to the index view without  
rails  
routing me to the create action.

ed

On 1/26/07, Rick O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

–  
Ed Hickey  
Developer  
Litmus Media  
816-533-0409  
removed\_email\_address@domain.invalid  
A Member of Think Partnership, Inc  
[www.ThinkPartnership.com](http://www.ThinkPartnership.com)  
Amex ticker symbol: THK

---

<div class="post-metadata">

**Author:** ![Ed\_Hickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/258eb7/32.png) [@Ed\_Hickey](https://www.ruby-forum.com/u/Ed_Hickey)\
**Post date:** [January 27, 2007, 12:05am UTC](https://www.ruby-forum.com/t/non-creative-posts-with-rest/83525/5 "2007-01-27T00:05:27Z")

</div>

On 1/26/07, Ed Hickey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a view with data that needs to be sorted and/or filtered.  
> Is the only solution to change my forms to GET instead of POST? Got to be a  
> better way…

If you’re just sorting/filtering the data, I would think it makes a  
lot more sense for the request to be a GET. Why don’t you want to  
change it like that?

Anyway, another option is to have some kind of Results or Query  
object. You POST to create on that object, and you could do whatever  
you want from there. Either just list the elements, save the query  
for later, etc.

Pat

---

<div class="post-metadata">

**Author:** ![Ed\_Hickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/258eb7/32.png) [@Ed\_Hickey](https://www.ruby-forum.com/u/Ed_Hickey)\
**Post date:** [January 29, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/non-creative-posts-with-rest/83525/6 "2007-01-29T18:45:47Z")

</div>

On 1/26/07, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> change it like that?  
> I guess I don’t really, I was just adverse to cluttering up the URL with  
> all  
> the filtering variables 🙂 I changed them form method to GET and it  
> works  
> as needed now.

Thanks for the help, everyone.

–  
Ed
