The resources
feature in routes
is very useful, but when starting with RoR, some features built on top of it can be difficult to interpret for yourself. The article will help you understand and be able to use routes effectively!
basic information about resources
Resources
help you to write a concise but complete description of the basic routes in your application. When you define a resources route
, it will create routes for all CRUD actions for you according to rails conventions .
resource
and resources
The first obvious difference between resources and resources is s , which means that the resource
is plural or singular.
Routes are created with resources
: 7 routes
Prefix | Verb | URI Pattern | Controller # action |
---|---|---|---|
users | GET | /users(.:format) | users # index |
POST | /users(.:format) | users # create | |
new_user | GET | /users/new(.:format) | users # new |
edit_user | GET | /users/:id/(.:format) | users # edit |
user | GET | /users/:id/(.:format) | users # show |
PUT / PATCH | /users/:id/(.:format) | users # update | |
DELETE | /users/:id/(.:format) | users # destroy |
Meanwhile, the Routes are created with the resource
:
Prefix | Verb | URI Pattern | Controller # action |
---|---|---|---|
new_user s | GET | /users/new(.:format) | users # new |
edit_user s | GET | /users/(.:format) | users # edit |
users | GET | /users/(.:format) | users # show |
PUT / PATCH | /users/(.:format) | users # update | |
DELETE | /users/(.:format) | users # destroy | |
POST | /users(.:format) | users # create |
As you can see there is no ID in route pattens when using resource
and resource
does not generate any route but is processed with action index.
So when to use resource
?
Ez! Sometimes you look up without referring to the ID. For example, you want /profile
always show the profile of the currently logged in user. In this case, you can use singular resource to map /profile
(instead of /profile/: id
) to action show
. Or for another example, a user usually belongs to a single organization, so to view that organization’s profile, we can have 2 routes:
/organizations/:id
/organization
#simply
Here, the second way of implementing the route makes more sense, right? You get the organization object from association with user:
1 2 3 | # <span class="token keyword">in</span> organizations#show @organization <span class="token operator">=</span> current_user <span class="token punctuation">.</span> organization |
only
and except
If you want to have only specific resource (s) routes, you can add the exceptions under “only” or “except”.
1 2 3 4 5 | Rails <span class="token punctuation">.</span> application <span class="token punctuation">.</span> routes <span class="token punctuation">.</span> draw <span class="token keyword">do</span> resources <span class="token operator">:</span> users <span class="token punctuation">,</span> only <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token operator">:</span> index <span class="token punctuation">]</span> resources <span class="token operator">:</span> customers <span class="token punctuation">,</span> except <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token operator">:</span> create <span class="token punctuation">,</span> <span class="token operator">:</span> show <span class="token punctuation">,</span> <span class="token operator">:</span> update <span class="token punctuation">,</span> <span class="token operator">:</span> destroy <span class="token punctuation">]</span> end |
Generated Routes would be:
1 2 3 4 5 6 | # Prefix Verb <span class="token constant">URI</span> Pattern Controller#Action # users <span class="token constant">GET</span> <span class="token operator">/</span> <span class="token function">users</span> <span class="token punctuation">(</span> <span class="token punctuation">.</span> <span class="token operator">:</span> format <span class="token punctuation">)</span> users#index # customers <span class="token constant">GET</span> <span class="token operator">/</span> <span class="token function">customers</span> <span class="token punctuation">(</span> <span class="token punctuation">.</span> <span class="token operator">:</span> format <span class="token punctuation">)</span> customers#index # new_customer <span class="token constant">GET</span> <span class="token operator">/</span> customers <span class="token operator">/</span> <span class="token keyword">new</span> <span class="token punctuation">(</span> <span class="token punctuation">.</span> <span class="token operator">:</span> format <span class="token punctuation">)</span> customers# <span class="token keyword">new</span> # edit_customer <span class="token constant">GET</span> <span class="token operator">/</span> customers <span class="token operator">/</span> <span class="token operator">:</span> id <span class="token operator">/</span> <span class="token function">edit</span> <span class="token punctuation">(</span> <span class="token punctuation">.</span> <span class="token operator">:</span> format <span class="token punctuation">)</span> customers#edit |
collection
route?
By using the collection, you can add additional routes to the controller. You just need to create a new collection route and define a method with the same name as the method on the controller. As you can see both ways of making COLLECTION work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Rails <span class="token punctuation">.</span> application <span class="token punctuation">.</span> routes <span class="token punctuation">.</span> draw <span class="token keyword">do</span> resources <span class="token operator">:</span> users <span class="token keyword">do</span> collection <span class="token keyword">do</span> <span class="token keyword">get</span> <span class="token operator">:</span> some_collection end <span class="token keyword">get</span> <span class="token operator">:</span> another_collection <span class="token punctuation">,</span> collection <span class="token operator">:</span> <span class="token boolean">true</span> end end # Generated Routes # Prefix Verb <span class="token constant">URI</span> Pattern Controller#Action # some_collection_users <span class="token constant">GET</span> <span class="token operator">/</span> users <span class="token operator">/</span> <span class="token function">some_collection</span> <span class="token punctuation">(</span> <span class="token punctuation">.</span> <span class="token operator">:</span> format <span class="token punctuation">)</span> users#some_collection # another_collection_users <span class="token constant">GET</span> <span class="token operator">/</span> users <span class="token operator">/</span> <span class="token function">another_collection</span> <span class="token punctuation">(</span> <span class="token punctuation">.</span> <span class="token operator">:</span> format <span class="token punctuation">)</span> users#another_collection |
member
in route?
member
are like collection
but they have an id in the generated route, which means that the route belongs to a member of the resource
And there are also two ways of creating MEMBER:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Rails <span class="token punctuation">.</span> application <span class="token punctuation">.</span> routes <span class="token punctuation">.</span> draw <span class="token keyword">do</span> resources <span class="token operator">:</span> users <span class="token keyword">do</span> member <span class="token keyword">do</span> <span class="token keyword">get</span> <span class="token operator">:</span> some_member end <span class="token keyword">get</span> <span class="token operator">:</span> another_member <span class="token punctuation">,</span> on <span class="token operator">:</span> <span class="token operator">:</span> member end end # Generated Routes # Prefix Verb <span class="token constant">URI</span> Pattern Controller#Action # some_member_user <span class="token constant">GET</span> <span class="token operator">/</span> users <span class="token operator">/</span> <span class="token operator">:</span> id <span class="token operator">/</span> <span class="token function">some_member</span> <span class="token punctuation">(</span> <span class="token punctuation">.</span> <span class="token operator">:</span> format <span class="token punctuation">)</span> users#some_member # another_member_user <span class="token constant">GET</span> <span class="token operator">/</span> users <span class="token operator">/</span> <span class="token operator">:</span> id <span class="token operator">/</span> <span class="token function">another_member</span> <span class="token punctuation">(</span> <span class="token punctuation">.</span> <span class="token operator">:</span> format <span class="token punctuation">)</span> users#another_member |
Alright! By using these methods, your code will be cleaner and more readable.
See you in the next post!