Non-creative POSTs with REST

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

On 1/26/07, Ed Hickey [email protected] 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

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. http://weblog.jamisbuck.org/2007/1/24/object-with_options


Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com

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. [email protected] wrote:

end
Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com


Ed Hickey
Developer
Litmus Media
816-533-0409
[email protected]
A Member of Think Partnership, Inc
www.ThinkPartnership.com
Amex ticker symbol: THK

On 1/26/07, Ed Hickey [email protected] 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

On 1/26/07, Pat M. [email protected] 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 :slight_smile: I changed them form method to GET and it
works
as needed now.

Thanks for the help, everyone.


Ed

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