什么是“资源"?在 Rails 中? [英] What is a "resource" in Rails?

查看:31
本文介绍了什么是“资源"?在 Rails 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

Is it the specific route? For example, map.resources maps the 7 RESTful "resources". So an example of one resource would be the call to, say, the index action of a particular class's controller?!?

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

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

还有别的吗?

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

Anyway, hopefully someone can set me straight...

推荐答案

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

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 generally a database table which is represented by a model, and acted on through a controller.

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

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

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

You act upon those resources by using the appropriate HTTP method when making calls to those resources. POST to the resource collection (/users) creates a new record; GET retrieves a list of resources (/users) or a specific user (/users/1). PUT updates a specific user (/users/1/), and DELETE destroys 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/1 always means "I'm interacting with the User that has ID #1", regardless of the action.

这篇关于什么是“资源"?在 Rails 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆