Ruby-on-rails 如何在同一个 Rails 控制器动作中处理多个 HTTP 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5583707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle multiple HTTP methods in the same Rails controller action
提问by Kevin Pang
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?
假设我想在同一个 URL 上同时支持 GET 和 POST 方法。我将如何在 rails 控制器操作中处理它?
回答by Jesse Wolgamott
You can check if it was a post using request.post?
您可以使用 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
回答by Dennis
Here's another way. I included example code for responding with 405 for unsupported methods and showing supported methods when the OPTIONS method is used on the URL.
这是另一种方式。我包含了示例代码,用于响应不支持的方法的 405,并在 URL 上使用 OPTIONS 方法时显示支持的方法。
In app/controllers/foo/bar_controller.rb
在 app/controllers/foo/bar_controller.rb
before_action :verify_request_type
def my_action
case request.method_symbol
when :get
...
when :post
...
when :patch
...
when :options
# Header will contain a comma-separated list of methods that are supported for the resource.
headers['Access-Control-Allow-Methods'] = allowed_methods.map { |sym| sym.to_s.upcase }.join(', ')
head :ok
end
end
private
def verify_request_type
unless allowed_methods.include?(request.method_symbol)
head :method_not_allowed # 405
end
end
def allowed_methods
%i(get post patch options)
end
In config/routes.rb
在 config/routes.rb
match '/foo/bar', to: 'foo/bar#my_action', via: :all
回答by jacr1614
Just need to use this, to use only get and post in the same route
只需要使用这个,在同一路由中只使用 get 和 post
resources :articles do
member do
match 'action_do-you_want', via: [:get, :post]
end
end
回答by jamesdelacruz
you can try this
你可以试试这个
match '/posts/multiple_action', to: 'posts#multiple_action', via: [:create, :patch, :get, :options]
回答by Ngoral
I would say, the best way is to create separate actions in the controller and state them in routes.
我想说,最好的方法是在控制器中创建单独的动作并在路由中声明它们。
# config/routes.rb
...
get '/my_action' => 'my#my_action_get'
post '/my_action' => 'my#my_action_post'
...
# app/controllers/my_controller.rb
...
def my_action_get
# do stuff like listing smth
end
def my_action_post
# do other stuff
end
In fact, the same logic is used by Rails by default: indexand createactions are both called by requests sent to the same paths (e.g. /articles), however, they have different request methods: GET /articlesrequest is redirected to the indexaction and lists all articles, and POST /articlesis redirected to the createaction and creates a new article.
实际上,Rails 默认使用相同的逻辑:index并且create动作都由发送到相同路径(例如/articles)的请求调用,但是它们具有不同的请求方法:GET /articles请求被重定向到index动作并列出所有文章,并且POST /articles是重定向到create操作并创建新文章。

