How to handle multiple HTTP methods in the same Rails controller action

前端 未结 6 1422
日久生厌
日久生厌 2021-01-31 02:50

Let\'s say I want to support both GET and POST methods on the same URL. How would I go about handling that in a rails controller action?

6条回答
  •  无人及你
    2021-01-31 02:58

    You can check if it was a post using request.post?

    if request.post?
      #handle posts
    else
      #handle gets
    end
    

    To get your routes to work:

    resources :photos do
      member do
        get 'preview'
        post 'preview'
      end
    end
    

提交回复
热议问题