Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between resource and resources in rails routing?

what is the difference between resource and resources in rails routing

 resource :geocoder 

and

 resources :posts 

What is real difference between them ?

like image 302
Manish Shrivastava Avatar asked Jul 06 '12 05:07

Manish Shrivastava


People also ask

What is difference between resource and resources in routes on 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 is resource and resources in Rails?

Rails provides RESTful routing for resources. Routes can either define single resource or plural resources to generate routes of the application. There is a logical difference that should be considered when selecting resource or resources when generating routers.

What are resources in Rails routes?

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 resources in routes RB?

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.


1 Answers

In essence, routing resources is when resources gives action abilities to a controller.

http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use

If a pluralized resources is used as a way to handle generic requests on any item, then a singular resource is a way to work on the current item at hand.

So in other words, if I have a collection of Apples, to retrieve a specific apple, I'd have to tell the router "Apples" what apple to retrieve by sending the ID of the apple. If I already have one Apple, then an ID is not needed.

Notice the differences between the two by looking at what actions (or routes) they have:

  • resources: Index, new, create, show, edit, update, destroy
  • resource: new, create, show, edit, update, destroy

In your example:

  1. The controller "geocoder" is a singular resource that you can use to edit, create, update, etc.
  2. The controller "posts", is a plural resource that will handle incoming generic posts that you can index, edit, create.. etc
like image 160
sksallaj Avatar answered Sep 24 '22 23:09

sksallaj