Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

ruby - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...