如何在同一个 Rails 控制器动作中处理多个 HTTP 方法
问题描述:
假设我想在同一个 URL 上同时支持 GET 和 POST 方法.我将如何在 Rails 控制器操作中处理它?
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?
答
你可以使用 request.post 来检查它是否是一个帖子?
You can check if it was a post using request.post?
if request.post?
#handle posts
else
#handle gets
end
要使您的路线生效:
resources :photos do
member do
get 'preview'
post 'preview'
end
end