简体   繁体   English

Rails-动作和http动词必须匹配吗?

[英]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_fileget ,也可以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. 如果需要,可以使用类似的路由来匹配GETPUT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Rails的控制器中访问路由信息(HTTP动词,动作名称等)? - How do I access the route information (HTTP verb, action name, etc . . .) from within a controller in Rails? Activeadmin自定义操作不遵守HTTP动词 - Activeadmin custom action not respecting http verb Rails - 从命名路由中提取HTTP谓词 - Rails - Extract HTTP Verb From Named Route rails:在redirect_to中指定HTTP动词 - rails: specify the HTTP verb in a redirect_to 如何向rails4添加自定义动词(http请求方法) - How to add custom verb (http request method ) to rails4 Rails:有没有一种方法可以使用错误的HTTP动词来“拯救”请求 - Rails: Is there a way to 'rescue' a request using the wrong HTTP verb 为HTTP Verb PATCH生成的实际URL Rails是什么? - What's the actual URL Rails generated for HTTP Verb PATCH? Rails - 从路由指定http动词PUT或DELETE - Rails - Specify http verb PUT or DELETE from the route 我有一个Rails博客,文章可以有多个类别-如何构造我的ArticlesController索引操作以匹配给定的类别ID? - I have a rails blog where articles can have multiple categories - how do I structure my ArticlesController index action to match on given category id? 如何使用“DELETE”HTTP动词发送请求? - How do you send a request with the “DELETE” HTTP verb?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM