Ruby-on-rails Rails 中的“资源”是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4686945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:05:21  来源:igfitidea点击:

What is a "resource" in Rails?

ruby-on-rails

提问by Meltemi

Dumb question but I have some lingering confusion of what, exactly, a "resource" is in Rails. The term is used everywhere but I get a funny feeling it might be being used rather loosely. It's referenced in the model, the controller and, quite literally, in routes.rb.

愚蠢的问题,但我对 Rails 中的“资源”究竟是什么有一些挥之不去的困惑。该术语随处可见,但我有一种有趣的感觉,它可能使用得相当松散。它在模型、控制器中被引用,确切地说,在routes.rb.

Is it the specific route? For example, map.resourcesmaps the 7 RESTful "resources". So an example of oneresource would be the call to, say, the indexaction of a particular class's controller?!?

是具体路线吗?例如,map.resources映射 7 个 RESTful“资源”。因此,一个资源的一个例子是对index特定类控制器的操作的调用?!?

Is it a reference to the whole page/object being retrieved? or perhaps, more narrowly, a database table? or the row being retreived?

它是对正在检索的整个页面/对象的引用吗?或者,更狭义的说,是一个数据库表?或正在检索的行?

Is it something else?

是别的吗?

Anyway, hopefully someone can set me straight...

无论如何,希望有人可以让我直截了当......

回答by Chris Heald

Any object that you want users to be able to access via URI and perform CRUD(or some subset thereof) operations on can be thought of as a resource. In the Rails sense, it is generallya database table which is represented by a model, and acted on through a controller.

您希望用户能够通过 URI 访问并对其执行CRUD(或其某些子集)操作的任何对象都可以视为资源。在 Rails 意义上,它通常是一个数据库表,由模型表示,并通过控制器进行操作。

For example, you might have a Userresource (with a userstable in your DB). This is represented by a Usermodel, is mapped to users_controllerwith map.resources :users(which then generates routes like /users(a collection of User resources) and /users/1(a specific User resource).

例如,您可能有一个User资源(users在您的数据库中有一个表)。这由User模型表示,映射到users_controllerwith map.resources :users(然后生成路由,如/users(用户资源的集合)和/users/1(特定用户资源)。

You act upon those resources by using the appropriate HTTP method when making calls to those resources. POSTto the resource collection (/users) creates a new record; GETretrieves a list of resources (/users) or a specific user (/users/1). PUTupdates a specific user (/users/1/), and DELETEdestroys that user. The URLs are the same, but the result (and controller action) may be different based on the HTTP verb. The idea, though is that /users/1always means "I'm interacting with the User that has ID #1", regardless of the action.

在调用这些资源时,您可以通过使用适当的 HTTP 方法对这些资源进行操作。POST到资源集合(/users)创建一个新记录;GET检索资源列表 ( /users) 或特定用户 ( /users/1)。PUT更新特定用户 ( /users/1/),并DELETE销毁该用户。URL 相同,但结果(和控制器操作)可能因 HTTP 动词而异。然而,这个想法/users/1总是意味着“我正在与 ID #1 的用户交互”,无论操作如何。

回答by JohnMetta

Here's a good articlediscussing how most developers think that "Resource" is synonomous with the database table, the argument, I guess, being that mapping to the resource is mapping the controller to that database table (or, with ActiveResource, to another REST url).

这是一篇很好的文章,讨论了大多数开发人员如何认为“资源”与数据库表是同义的,我猜,参数是映射到资源是将控制器映射到该数据库表(或者,使用 ActiveResource,映射到另一个 REST url )。

Basically, I think a "resource" is "persisted data." map.resourcesmaps the 7 RESTful actionsto a particular suite of persisted data.

基本上,我认为“资源”是“持久化数据”。map.resources将 7 个 RESTful操作映射到一组特定的持久数据。

But I haven't thought about it too much in depth. Good question!

但我并没有想太多深入。好问题!

回答by Ken

I think they probably mean it in the general web sense, i.e., Resource (Web):

我认为他们可能是一般网络意义上的意思,即Resource (Web)

the referent of any Uniform Resource Identifier

任何统一资源标识符的指称

I don't think it has anything to do with database tables.

我认为它与数据库表没有任何关系。

回答by Brian Joseph Spinos

open your model folder, that is a hint of what resources you have! example: users, pictures, comments...

打开您的模型文件夹,这是您拥有的资源的提示!例如:用户、图片、评论...

回答by Palak Singhal

A lot of people here say that resources refer to the database tables you have. It might be true sometimes but not necessarily true always. I could give you a lot of examples where you don't have a corresponding table in your database for a particular resource. Hence asssociating it with tables is rather wrong.

这里很多人说资源是指您拥有的数据库表。有时可能是正确的,但不一定总是正确的。我可以给你很多例子,你的数据库中没有特定资源的相应表。因此,将它与表相关联是相当错误的。

I would define a resource as a route which maps to related requests. So instead of declaring separate routes for the actions you want to do you can simply declare them using a resourceful route.In Rails, a resourceful route provides a mapping between HTTP requestsand URLsto controller actions.

我会将资源定义为映射到相关请求的路由。因此,而不是宣布采取的行动不同的途径,你想干什么,你可以使用一个足智多谋的route.In Rails的简单声明它们,一个足智多谋的路由提供之间的映射HTTP requests,并URLscontroller actions

So say you define resources :usersin config/routes.rb.
You can now use a number of helpers to the controllers in your application like edit_user_pathwhich returns users/edit.

所以说你resources :usersconfig/routes.rb.
您现在可以在您的应用程序中对控制器使用许多帮助程序,例如edit_user_pathwhich returns users/edit

回答by Wolf

Here's a good link: https://api.rubyonrails.org/v5.2.1/classes/ActionDispatch/Routing/Mapper/Resources.html

这是一个很好的链接:https: //api.rubyonrails.org/v5.2.1/classes/ActionDispatch/Routing/Mapper/Resources.html

Which basically says: 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 :photos

资源:照片