What is a "resource" in Rails?

Ruby on-Rails

Ruby on-Rails Problem Overview


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.

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?

Is it something else?

Anyway, hopefully someone can set me straight...

Ruby on-Rails Solutions


Solution 1 - Ruby on-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.

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.

Solution 2 - Ruby on-Rails

Here's a good article discussing 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).

Basically, I think a "resource" is "persisted data." map.resources maps the 7 RESTful actions to a particular suite of persisted data.

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

Solution 3 - Ruby on-Rails

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

> the referent of any Uniform Resource Identifier

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

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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 requests and URLs to controller actions.

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

Solution 6 - Ruby on-Rails

Here's a good link: 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMeltemiView Question on Stackoverflow
Solution 1 - Ruby on-RailsChris HealdView Answer on Stackoverflow
Solution 2 - Ruby on-RailsJohnMettaView Answer on Stackoverflow
Solution 3 - Ruby on-RailsKenView Answer on Stackoverflow
Solution 4 - Ruby on-RailsBrian Joseph SpinosView Answer on Stackoverflow
Solution 5 - Ruby on-RailsPalak SinghalView Answer on Stackoverflow
Solution 6 - Ruby on-RailsWolfView Answer on Stackoverflow