开发者

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, wh开发者_如何学Cile also preserving the ability to create a new record of that resource?

Thanks much.


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


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

resources :my_things do
  post :index
end


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜