Search code examples
ruby-on-railsdevise

How to embed a route resource in a Devise route in Rails 6


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!


Solution

  • 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'
      }