Difference between resource and resources in rails routing?

25,948

Solution 1

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

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

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

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.

Share:
25,948

Related videos on Youtube

Manish Shrivastava
Author by

Manish Shrivastava

Manish is the Ruby on Rails and React Expert Developer. He loves coding. Interesting code area: ROR Web Application / ML Code / AI Code. He have very good experience (10+ years) in technologies listed below: Backend: Ruby ( Ruby On Rails / Sinatra ) * Frontend: React / Vue.js JavaScript ( CoffeeScript/ JQuery / ProtoType / Plain JavaScript ) CSS/CSS3 (SASS / SCSS / Plain CSS ) HTML/HTML5 (HAML / Plain HTML) Database: MySql, MongoDB, PostgreSQL Platform: Mac, Ubuntu, Windows. Education B.E. (RGPV University), M.B.A.(IT- PGDIT Symbiosis University) Sites Professional http://stackoverflow.com/users/1133932/manish-shrivastava https://github.com/manish-shrivastava https://www.linkedin.com/in/ceomanish/ http://www.techgig.com/railsdeveloper http://www.manishshrivastava.com Social https://plus.google.com/+ManishShrivastavaa http://www.twitter.com/engineer_manish http://www.facebook.com/er.manishshrivastava Others http://www.youtube.com/er.manishshrivastava http://www.flickr.com/photos/manish-shrivastava http://manish-shrivastava.deviantart.com http://www.behance.net/manishshrivastava http://www.vimeo.com/manishshrivastava http://www.last.fm/user/engineer_manish http://www.manishshrivastava.tumblr.com

Updated on July 09, 2022

Comments

  • Manish Shrivastava
    Manish Shrivastava almost 2 years

    what is the difference between resource and resources in rails routing

     resource :geocoder
    

    and

     resources :posts
    

    What is real difference between them ?

  • illusionist
    illusionist over 8 years
    My confusion: what is the point in creating singular resources; lets say we have defined resource :geocoder; does it mean that we are gonna have a single geocoder record in a whole table called geocoders?
  • sksallaj
    sksallaj over 8 years
    In the link to the article (which was updated long since I posted this), they actually gave a good example. A singular resource called /profile (instead of /profile:id), would be useful to always show profile of the currently logged in user. So a singular geocoder could belong to a current user's location, where as plural geocoders could belong to a collection of nearby places. It'd be up to you if you want to include your own geocode into that collection.