区分 Restful 路线和非 restful 路线
Differentiate Restful routes and non-restful routes
据我所知,restful路由是基于REST架构的路由。并且 rails 默认使用 restful 路由。 resource
关键字为我们定义了7条restful条路线。而如果我必须定义自定义路线,
resource :photos do
memeber do
get 'preview' #non-restful route
end
end
现在 preview
路线被定义为非 restful 路线。不是RESTful路线吗?
我的问题是,我们如何区分RESTful路由和非RESTful路由?你能举一个自定义 restful 路线和非 restful 路线的例子吗?
定义 Restful 网络服务没有硬性规定,但 Rails 为您提供了一种称为 resources
的方法来生成 Restful 网络服务。但这要视情况而定。
你可以看看A Beginner’s Guide to HTTP and REST。它深入描述了请求如何属于 Restful 请求的类别。
在许多应用程序中,您还会看到非 RESTful 路由,它明确地将 URL 的各个部分连接到特定的操作。例如,
map.connect 'parts/:number', :controller => 'inventory', :action => 'show'
另一方面,当您在应用程序的 route.rb
中指定路由时使用 resources
或 resource
,您将得到 RESTful URLs对于他们,例如:
map.resources :photos
将产生:
Verb URL controller action used for
GET /photos Photos index display a list of all photos
GET /photos/new Photos new return an HTML form for creating a new photo
POST /photos Photos create create a new photo
GET /photos/1 Photos show display a specific photo
GET /photos/1/edit Photos edit return an HTML form for editing a photo
PUT /photos/1 Photos update update a specific photo
DELETE /photos/1 Photos destroy delete a specific photo
据我所知,restful路由是基于REST架构的路由。并且 rails 默认使用 restful 路由。 resource
关键字为我们定义了7条restful条路线。而如果我必须定义自定义路线,
resource :photos do
memeber do
get 'preview' #non-restful route
end
end
现在 preview
路线被定义为非 restful 路线。不是RESTful路线吗?
我的问题是,我们如何区分RESTful路由和非RESTful路由?你能举一个自定义 restful 路线和非 restful 路线的例子吗?
定义 Restful 网络服务没有硬性规定,但 Rails 为您提供了一种称为 resources
的方法来生成 Restful 网络服务。但这要视情况而定。
你可以看看A Beginner’s Guide to HTTP and REST。它深入描述了请求如何属于 Restful 请求的类别。
在许多应用程序中,您还会看到非 RESTful 路由,它明确地将 URL 的各个部分连接到特定的操作。例如,
map.connect 'parts/:number', :controller => 'inventory', :action => 'show'
另一方面,当您在应用程序的 route.rb
中指定路由时使用 resources
或 resource
,您将得到 RESTful URLs对于他们,例如:
map.resources :photos
将产生:
Verb URL controller action used for
GET /photos Photos index display a list of all photos
GET /photos/new Photos new return an HTML form for creating a new photo
POST /photos Photos create create a new photo
GET /photos/1 Photos show display a specific photo
GET /photos/1/edit Photos edit return an HTML form for editing a photo
PUT /photos/1 Photos update update a specific photo
DELETE /photos/1 Photos destroy delete a specific photo