Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby on Rails, what does "resource" mean?

I see the word resource in many different places like: resource Routing, resourceful controller, and resources: photos. What does resource actually mean?

One more question: What does RESTful route mean?

like image 544
Misha Moroshko Avatar asked Dec 14 '10 13:12

Misha Moroshko


People also ask

What is a resource in 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.

What is resource and resources in Rails?

Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.

What does resources do in Rails routes?

Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. A single call to resources can declare all of the necessary routes for your index , show , new , edit , create , update , and destroy actions.

What is difference between resource and resources?

Declaring a resource or resources generally corresponds to generating many default routes. resource is singular. resources is plural.


1 Answers

That's a big question!

I'd start here to better understand what 'resource' or 'resources' does as it relates to routing: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

The short of it is that that it formalizes a set of actions (for a specific controller) invoked by URL/HTTP Verb pairs that are responsible for modifying the state of a given resource. Think of resources as nouns: Order, LineItem, Offer and think about what you might want to do with those nouns: typically create them, delete them, modify them, retrieve some set of them, etc. As such, resources are often (but certainly don't have to be) your core model objects and/or some composite representation of those core models.

Again - the Rails Guides summarize what resourceful routes Rails very succinctly in Section 2.1 of the above link: 'In Rails, a resourceful route provides a mapping between HTTP verbs and URLs and controller actions'

If you're unfamiliar with REST, Wikipedia has some decent - but not exhaustive - coverage on the architecture: http://en.wikipedia.org/wiki/Representational_State_Transfer.

like image 166
Cory Avatar answered Nov 11 '22 12:11

Cory