0

Under normal circumstances if one wants to embed resources within other resources in Ruby on Rails in the routes.rb file it would look like this:

# routes.rb

resources :parents do
  resources :children
end

The above will allow for a url like http://localhost:3000/parents/1/children.

My question is how to achieve the same result with the default devise_for :parents that exists in my routes.rb file?

I tried:

# routes.rb

devise_for :parents do
  resources :children
end

and it did not work properly.

Any help is greatly appreciated!

jmvbxx
  • 976
  • 3
  • 13
  • 21
  • [I found the answer in a previous report](https://stackoverflow.com/questions/27712947/nested-resources-in-devise) – jmvbxx Aug 22 '20 at 02:22
  • Can you add a detailed answer as an answer? That can help a lot of people since this specifically says Devise instead of normal routes, though answer will be more or less the same. – Prabin Poudel Aug 22 '20 at 03:15

1 Answers1

1

devise_for only creates the routes related to signing up and in, so you would still use

resources :parents do 
  resources :children
 end

in your routes for nested resource paths. There is a detailed answer here: Nested Resource with Devise - Rails3

If you generate the Devise controllers and views, you also need to specify those in your routes like this

  devise_for :parents, controllers: {
    sessions: 'parents/sessions',
    registrations: 'parents/registrations'
  }