为 HTTP Verb PATCH 生成的实际 URL Rails 是多少?

What's the actual URL Rails generated for HTTP Verb PATCH?

新手请多多包涵。我了解 Rails 如何通过 GET 为简单的 URL 链接(如 localhost:3000/rooms/11/listing)提供简单的请求。格式很简单,如路由 table 中所述。但是,当涉及到 PATCH、PUT、DELETE 和 CREATE 时,我感到很困惑。例如,下面带有参数的输出是我单击“保存”按钮时的结果。我的问题是,当我单击“保存”按钮时 Rails 生成的实际 URL 是什么?

Started PATCH "/rooms/11" for 127.0.0.1 at 2019-08-20 05:25:32 +0800
(0.8ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  ↳ /usr/local/rvm/gems/ruby-2.6.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Processing by RoomsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"JHiCJ6HoTsgd8SGgLTFiZ9+J9hS9U8hHGvjKf4Lz3uieQ8OO2eFqFEM/D5xocHp/Nd3eA0az9k+okrmNe65BYg==", "room"=>{"home_type"=>"Apartment", "room_type"=>"Private", "accommodate"=>"3", "bed_room"=>"4", "bath_room"=>"3"}, "commit"=>"Save", "id"=>"11"}

我从控制台知道

app.room_path(11)
 => "/rooms/11" 

这是Rails生成的吗URLlocalhost:3000/rooms/11{"utf8"=>"✓", "authenticity_token"=>"JHiCJ6HoTsgd8SGgLTFiZ9+J9hS9U8hHGvjKf4Lz3uieQ8OO2eFqFEM/D5xocHp/Nd3eA0az9k+okrmNe65BYg==", "room"=>{"home_type"=>"Apartment", "room_type"=>"Private", "accommodate"=>"3", "bed_room"=>"4", "bath_room"=>"3"}, "commit"=>"Save", "id"=>"11"}?

不是,app.room_path(11)生成的URL是http://localhost:3000/rooms/11

PATCH、PUT、DELETE 和 POST 称为 HTTP 动词。 CREATE 不是 HTTP 动词。

这些动词之一与您的请求一起出现,Rails 路由器使用它将请求路由到正确的控制器和操作。

请求可以有参数,就像你在这里展示的那样: {"utf8"=>"✓", "authenticity_token"=>"JHiCJ6HoTsgd8SGgLTFiZ9+J9hS9U8hHGvjKf4Lz3uieQ8OO2eFqFEM/D5xocHp/Nd3eA0az9k+okrmNe65BYg==", "room"=>{"home_type"=>"Apartment", "room_type"=>"Private", "accommodate"=>"3", "bed_room"=>"4", "bath_room"=>"3"}, "commit"=>"Save", "id"=>"11"}

当您单击“保存”按钮时,您的浏览器使用 HTTP 动词 POST 请求 http://localhost:3000/rooms/11。参数在请求的正文中编码。

了解更多信息的好地方是 Rails routing guide

这是 rake routes 命令的结果。如您所见,GET、PATCH 和 PUT 共享相同的生成 URL(/rooms/:id,在您的示例中 /rooms/11)。从 Rails 4.0 开始,PATCH 是更新操作的默认动词。将表单发送到路由时会触发更新操作。