Rails 3: Using a POST form on an index page?

In my Rails 3.0 application, I have a series of very large search forms on my resource: an index page requiring the use of POST instead of GET.

Currently, the application sends a POST request to resource # create when I want it to go to resource index #. I understand this is a RESTful route, but I need to redefine it. How can I do this while also maintaining the ability to create a new record for this resource?

Many thanks.

+5
source share
3 answers

You are better off having a “search” action, which is only post-only, and then displays the index template, for example:

class MyController < ...
  def search
     @my_things = MyThing.find_with_search_params(params[:search]) 
     render :action => :index
  end
end
+2
source

, Rails 3:

resources :my_things do
  post :index
end
+3

So, you want your “created” action endpoint in the controller to do 2 things: respond to the search and create as well? A bad idea, but the solution can be as simple as using the if condition in the create action for one or the other. If this is not a satisfactory answer, feel free to clarify your question a bit.

0
source

All Articles