用户资源扩展

User Resources Expanded

为了更清楚地了解"resources"在Ruby on Rails routes.rb文件中究竟做了什么,我想在它下面写下它的确切代码正在更换。

当我 运行 rake routes 我得到这个:

       users GET    /users(.:format)          users#index
             POST   /users(.:format)          users#create
    new_user GET    /users/new(.:format)      users#new
   edit_user GET    /users/:id/edit(.:format) users#edit
        user GET    /users/:id(.:format)      users#show
             PATCH  /users/:id(.:format)      users#update
             PUT    /users/:id(.:format)      users#update
             DELETE /users/:id(.:format)      users#destroy

谁能帮我填一下下面的空白,这样我就可以更清楚地理解这一点:

  resources 'users'
  # get 'users' => 'users#index"
  # post ...
  # get ...
  # get ...
  # patch ...
  # put ...
  # delete ...

等效代码可以表示为:

get 'users', to: 'users#index'
post 'users', to: 'users#create'
get 'users/new', to: 'users#new', as: 'new_user'
get 'users/:id/edit', to: 'users#edit', as: 'edit_user'
get 'users/:id', to: 'users#show', as: 'user'
patch 'users/:id', to: 'users#update'
put 'users/:id', to: 'users#update'
delete 'users/:id', to: 'users#destroy'

Brad werth的回答正是您所需要的。


为了给您更多背景信息,您还需要了解 resourceful routing system 在 Rails 中的工作方式...

Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.

基本上,每次调用 resources 时,您都在告诉 rails 为围绕 "resourceful" 原则设计的控制器构建一组路由。

"Resourceful" 通过 Internet 进行的操作由维基百科定义如下:

HTTP functions as a request-response protocol in the client-server computing model. A web browser, for example, may be the client and an application running on a computer hosting a web site may be the server. The client submits an HTTP request message to the server. The server, which provides resources such as HTML files and other content, or performs other functions on behalf of the client, returns a response message to the client. The response contains completion status information about the request and may also contain requested content in its message body.

当您了解 Ruby/Rails 是 object orientated 时,所有这些都有意义。这意味着 您在应用程序中所做的一切 都必须解决 "objects".

的初始化和维护问题

对象基本上就是您的 模型 - 它们是通过您的控制器操作创建、编辑和销毁 (CRUD -- create read update destroy) 的。因此,要给你一套标准化的路线,你可以使用以下方法:

如果您想像 brad 概述的那样查看您的路线,您需要 运行 rake routes

--

好资源在这里:https://softwareengineering.stackexchange.com/questions/120716/difference-between-rest-and-crud