What is a “resource” in Rails?

前端 未结 6 1709
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 10:36

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

6条回答
  •  借酒劲吻你
    2020-12-12 10:56

    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.

    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).

    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.

提交回复
热议问题