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:
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…
With a little creativity, or Jamis Buck'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:
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.
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.
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.