Difference between resource and resources in rails routing?
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:
- The controller "geocoder" is a singular resource that you can use to edit, create, update, etc.
- The controller "posts", is a plural resource that will handle incoming generic posts that you can index, edit, create.. etc
Difference between resource and resources methods
Actually you are right, resource
should not create an index action, unless you ask for the index action explicitly, this way:
resource :orders, :only => [:index, :create, :show]
Helpers should differ too, but not that much as in your example, because the convention is to use a singular form with the resource
method, and the plural with the resources
resources :orders
=> rake routes
orders GET /orders(.:format) orders#index
POST /orders(.:format) orders#create
new_order GET /orders/new(.:format) orders#new
edit_order GET /orders/:id/edit(.:format) orders#edit
order GET /orders/:id(.:format) orders#show
PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
resource :order
=> rake routes
order POST /order(.:format) orders#create
new_order GET /order/new(.:format) orders#new
edit_order GET /order/:id/edit(.:format) orders#edit
GET /order/:id(.:format) orders#show
PUT /order/:id(.:format) orders#update
DELETE /order/:id(.:format) orders#destroy
And the logical difference is to declare you logically can't have the plural for resource in your app, for example Admin or whatever
What is the difference between resources and namespace in rails nested routing
Check out the difference using rake routes
.
This definition with a namespace:
namespace :alpha do
resources :posts
end
results in the following routes:
Prefix Verb URI Pattern Controller#Action
alpha_posts GET /alpha/posts(.:format) alpha/posts#index
POST /alpha/posts(.:format) alpha/posts#create
new_alpha_post GET /alpha/posts/new(.:format) alpha/posts#new
edit_alpha_post GET /alpha/posts/:id/edit(.:format) alpha/posts#edit
alpha_post GET /alpha/posts/:id(.:format) alpha/posts#show
PATCH /alpha/posts/:id(.:format) alpha/posts#update
PUT /alpha/posts/:id(.:format) alpha/posts#update
DELETE /alpha/posts/:id(.:format) alpha/posts#destroy
As you can see, the only thing that is different from a plain resources
route set is the addition of /alpha
prefix.
Now for the two-level resources
routes definition:
resources :alpha do
resources :posts
end
which results in:
Prefix Verb URI Pattern Controller#Action
alpha_posts GET /alpha/:alpha_id/posts(.:format) posts#index
POST /alpha/:alpha_id/posts(.:format) posts#create
new_alpha_post GET /alpha/:alpha_id/posts/new(.:format) posts#new
edit_alpha_post GET /alpha/:alpha_id/posts/:id/edit(.:format) posts#edit
alpha_post GET /alpha/:alpha_id/posts/:id(.:format) posts#show
PATCH /alpha/:alpha_id/posts/:id(.:format) posts#update
PUT /alpha/:alpha_id/posts/:id(.:format) posts#update
DELETE /alpha/:alpha_id/posts/:id(.:format) posts#destroy
alpha_index GET /alpha(.:format) alpha#index
POST /alpha(.:format) alpha#create
new_alpha GET /alpha/new(.:format) alpha#new
edit_alpha GET /alpha/:id/edit(.:format) alpha#edit
alpha GET /alpha/:id(.:format) alpha#show
PATCH /alpha/:id(.:format) alpha#update
PUT /alpha/:id(.:format) alpha#update
DELETE /alpha/:id(.:format) alpha#destroy
As you can see, alpha
becomes a top-level resource with all 8 RESTful routes. posts
, in turn, become second-level resource, accessible only through the route to a specific alpha
object.
Read more in Rails Routing from the Outside In.
You might also find interesting the scope
option. Read about the difference between scope
and namespace
in Scoping Rails Routes blog post.
Difference between resource and resources methods
Actually you are right, resource
should not create an index action, unless you ask for the index action explicitly, this way:
resource :orders, :only => [:index, :create, :show]
Helpers should differ too, but not that much as in your example, because the convention is to use a singular form with the resource
method, and the plural with the resources
resources :orders
=> rake routes
orders GET /orders(.:format) orders#index
POST /orders(.:format) orders#create
new_order GET /orders/new(.:format) orders#new
edit_order GET /orders/:id/edit(.:format) orders#edit
order GET /orders/:id(.:format) orders#show
PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
resource :order
=> rake routes
order POST /order(.:format) orders#create
new_order GET /order/new(.:format) orders#new
edit_order GET /order/:id/edit(.:format) orders#edit
GET /order/:id(.:format) orders#show
PUT /order/:id(.:format) orders#update
DELETE /order/:id(.:format) orders#destroy
And the logical difference is to declare you logically can't have the plural for resource in your app, for example Admin or whatever
Difference between get and resource with except
http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Resources.html
There is no difference - it will act in the same way. Choose the one with more comfortable syntax for you.
Resources vs namespace in Rails routes
As per the Rails guide routing section
Resources:
Resource routing allows you to quickly declare all of the common
routes for a given resourceful controller. Instead of declaring
separate routes for your index, show, new, edit, create, update and
destroy actions, a resourceful route declares them in a single line of
code.
Namespace:
You may wish to organize groups of controllers under a namespace. Most
commonly, you might group a number of administrative controllers under
an Admin:: namespace. You would place these controllers under the
app/controllers/admin directory, and you can group them together in
your router.
Eg:
namespace :admin do
resources :articles, :comments
end
But, I think what you meant was to choose between collection and namespacing.
It's like this, namespacing would be a better option if you are planning to have more routes for that app. Else, you can just use it as a collection.
Rails: Difference between declaring a route directly inside the `resources` block vs. enclosing it with a `member` block
First of all, if we write the member
block or directly write the get routes inside the resources
both are considered as member routes.
Its the rails convention to differentiate between both of the routes. If we write the member block it is considered that all the routes declared within that block are declared from the member block explicitly.
resources :animals do
member do
get 'info'
end
end
info_animal GET /animals/:id/info(.:format) animals#info
But if we directly declare get
or other routes inside the resources
block this will also create the same member
route except that the resource id
value will be available in params[:animal_id]
instead of params[:id]
. Route helpers will also be renamed from info_animal_url
and info_animal_path
to animal_info_url
and animal_info_path
. I think this is to make difference that request is not coming from the member
block.
resources :animals do
get 'info'
end
animal_info GET /animals/:animal_id/info(.:format) animals#info
If we write get
route with the on:
option with value :member
inside the resources directly then this will be treated same as the member block route
resources :animals do
get 'info', on: :member
end
info_animal GET /animals/:id/info(.:format) animals#info
Related Topics
What Is '-Mix' in a Ruby Regular Expression
How to Change Column Type in Heroku
Rvm Does Not Install Ruby 1.9.2 on Snow Leopard: 'Error Running 'Make '
Windows: Rails: Error Installing Bson_Ext
Remove from the Array Elements That Are Repeated
Ruby Capitalize Every Word First Letter
Ruby Inject with Initial Being a Hash
Storing Arrays in Database Using Activerecord
Rack::Request - How to Get All Headers
Convert String into Array in Rails
How to Insert a String into a Textfile
Show Full Path Name of the Ruby File When It Get Loaded
Replace "&" to "\&" in Ruby Seems Impossible
Throw Exception When Re-Assigning a Constant in Ruby
Ruby Variable (Array) Assignment Misunderstanding (With Push Method)