Andrew
Andrew

Reputation: 77

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

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

Currently, the app is routing the POST request to resource#create, when I want it to route to resource#index. I realize this is the RESTful route, but need to override it. How can I do that, while also preserving the ability to create a new record of that resource?

Thanks much.

Upvotes: 5

Views: 6035

Answers (3)

tomaszbak
tomaszbak

Reputation: 8287

You can use index, just add this in Rails 3 routes:

resources :my_things do
  post :index
end

Upvotes: 2

Taryn East
Taryn East

Reputation: 27747

You're better off having a "search" action that is post-only - and then renders the index template eg:

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

Upvotes: 3

Aditya Sanghi
Aditya Sanghi

Reputation: 13433

So you want your "create" action end-point in the controller to do 2 things - Respond to search and do the create also? Bad idea, but the solution might be as simple as using an "if" condition in the create action to do one or the other. If its not a satisfactory answer, feel free to clarify your question a bit more.

Upvotes: 0

Related Questions