[英]Rails - do action and http verb have to match?
A custom action export_file
is defined in routes.rb: 在export_file
中定义了一个自定义动作export_file:
resource :payment_requests do
collection do
get :export_file
end
end
In a form_tag
, can we use method: put
for export_file even though the export_file
is a get
? 在form_tag
,我们可以使用以下method: put
即使export_file
是get
,也可以method: put
export_file?
<%= form_tag export_file_payment_requests_path(format: 'csv'), method: :put do %>
......
<%= submit_tag 'CSV' %>
<% end %>
It seems OK to use put
method for get
action according to http document. 这似乎OK使用put
方法get
根据HTTP文档操作。
Essentially, yes that's fine - you absolutely can send a PUT
request from a form in Rails. 本质上,是的,这很好-您绝对可以从Rails中的表单发送PUT
请求。
However, if you specify a route in your routes.rb
file as being a get
request, it will only ever match it using the HTTP GET verb. 但是,如果您在routes.rb
文件中将路由指定为get
请求,则该路由只会使用HTTP GET动词与之匹配。
Take a look at the output from rake routes
: 看一下rake routes
的输出:
Prefix Verb URI Pattern Controller#Action
api_v1_search_simple GET /api/v1/search/simple(.:format) api/v1/search#simple
new_user_session GET /auth/login(.:format) devise/sessions#new
user_session POST /auth/login(.:format) devise/sessions#create
destroy_user_session DELETE /auth/logout(.:format) devise/sessions#destroy
user_password POST /auth/password(.:format) devise/passwords#create
new_user_password GET /auth/password/new(.:format) devise/passwords#new
edit_user_password GET /auth/password/edit(.:format) devise/passwords#edit
PATCH /auth/password(.:format) devise/passwords#update
PUT /auth/password(.:format) devise/passwords#update
user_confirmation POST /auth/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /auth/confirmation/new(.:format) devise/confirmations#new
GET /auth/confirmation(.:format) devise/confirmations#show
root GET / dashboards#show
You see how it lists the Verb
it will match against? 您看到它如何列出将与之匹配的Verb
吗?
If you want to send a PUT
request and have rails match it against a particular controller and action, then you should specify it as a put
request in your routes.rb
file. 如果要发送一个PUT
请求,并使它与特定的控制器和动作相匹配,则应在routes.rb
文件routes.rb
其指定为put
请求。
You do have the option of using the match
wildcard to define a route, but that is generally considered a bad thing to do, since it opens up your application for misuse. 您确实可以选择使用match
通配符来定义路由,但这通常被认为是不好的事情,因为它会使您的应用程序被滥用。
This guide is really useful: http://guides.rubyonrails.org/routing.html 该指南非常有用: http : //guides.rubyonrails.org/routing.html
If you read section 3.7 , you'll see that you can use the match
keyword along with the via
attribute to restrict what verbs your route should match against. 如果您阅读了3.7节 ,您将看到可以将match
关键字与via
属性一起使用,以限制您的路线应与之match
动词。 Such as this: 如:
match 'photos', to: 'photos#show', via: [:get, :post]
You could use a route like that to match against both GET
and PUT
if you need it. 如果需要,可以使用类似的路由来匹配GET
和PUT
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.