簡體   English   中英

在 routes.rb 中獲取、匹配和資源

[英]get, match and resources in routes.rb

我是 Rails 的新手。 當我使用 routes.rb 中的資源時,我發現它很奇怪,在我將頁面重定向到 controller/index 后,它呈現了 controller/show 。

我知道GET controller/actionmatch "controller/action", :to => "controller/action"

我認為關於重定向的奇怪事情發生在我身上,類似於 GET 和 Match。

所以我想知道資源到底是什么意思,我可以使用一些簡單的匹配來做同樣的事情嗎?

resources是生成REST接口所需的七個路由的快捷方式。

resources :widgets相當於寫作

get    "widgets"          => "widgets#index",   :as => 'widgets'
get    "widgets/:id"      => "widgets#show",    :as => 'widget'
get    "widgets/new"      => "widgets#new",     :as => 'new_widget'
post   "widgets"          => "widgets#create",  :as => 'widgets'
get    "widgets/:id/edit" => "widgets#edit",    :as => 'edit_widget'
patch  "widgets/:id"      => "widgets#update",  :as => 'widget'
put    "widgets/:id"      => "widgets#update",  :as => 'widget'
delete "widgets/:id"      => "widgets#destroy", :as => 'widget'

它只是為您省去了麻煩。

順便說一下, getmatch並不完全相同。 getpostputdelete是將路由限制為單個 HTTP 動詞的快捷方式。 下面的兩個路由定義是等效的。

match 'foo' => 'controller#action', :method => :get
get   'foo' => 'controller#action'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM