Difference between resource and resources in rails routing?

Ruby on-RailsRubyRuby on-Rails-3Routes

Ruby on-Rails Problem Overview


what is the difference between resource and resources in rails routing

 resource :geocoder

and

 resources :posts

What is real difference between them ?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

Singular Resources:

Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user.

Or, Normally your currently logged-In user belongs to a single organization, so to goto his/her organization profile page there can be two routes

#1
/organizations/:id

#2
/organization #simply

Here, the later implementation makes more sense; isnot it? you get the organization object from association

# in organizations#show
@organization = current_user.organization

To define such singular resource you use resource method: Example

# in routes.rb
resource :organization

creates six different routes in your application, all mapping to the Organizations controller:

enter image description here

whereas, you define plural resources using resources method

resources :organizations

enter image description here

Solution 3 - Ruby on-Rails

http://guides.rubyonrails.org/routing.html#singular-resources

> Sometimes, you have a resource that clients always look up without > referencing an ID. For example, you would like /profile to always show > the profile of the currently logged in user. In this case, you can use > a singular resource to map /profile (rather than /profile/:id) to the > show action.

A good way to see it is that resource does not have an index action, since it's suppose to be just one.

Solution 4 - Ruby on-Rails

i think just the index view.

also there have been reported issues with routing with the resource helper and form helpers. personally, i use the syntax:

resources :someresource, except: :index 

in order to avoid the reported bugs.

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
QuestionManish ShrivastavaView Question on Stackoverflow
Solution 1 - Ruby on-RailssksallajView Answer on Stackoverflow
Solution 2 - Ruby on-RailsillusionistView Answer on Stackoverflow
Solution 3 - Ruby on-RailsIsmaelView Answer on Stackoverflow
Solution 4 - Ruby on-RailsHanView Answer on Stackoverflow