Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested resource and restricting the Routes Created :only and :except

Good day all,

Can someone kindly assist me with nested resources and its best practice.

I would like to restrict my :events route to only :show and :index, is this the correct way of doing it?

resources :events do
    resources :registrations, :except => [:index]
end

resources :events, :only => [:index, :show]

Is this the best way or the way more Rubyist would handle such thing? I've added two lines of resources :events or is there a way to combine it all in 1 block?

Thanks in advance.

like image 347
Wasabi Developer Avatar asked Nov 02 '12 08:11

Wasabi Developer


People also ask

What is a nested resource?

Nesting resources provide REST API consumers an easy and efficient way to manage data by allowing the consumer to send and receive only the required object. The nested resource must be a business object, that is, it must still represent a complete business object.

What are nested routes in Rails?

In a nested route, the belongs_to (child) association is always nested under the has_many (parent) association. The first step is to add the nested routes like so: In the above Rails router example, the 'index' and 'show' routes are the only nested routes listed (additional or other resources can be added).

What are resource routes?

Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. A single call to resources can declare all of the necessary routes for your index , show , new , edit , create , update , and destroy actions.

What is the difference between resources and resource in Rails?

Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.


1 Answers

Yes. You can combine it in one block like:

resources :events, only: [:index, :show] do
    resources :registrations, except: :index
end
like image 133
Sergey Kishenin Avatar answered Sep 28 '22 17:09

Sergey Kishenin