Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Setting Devise login page as my unauthenticated root, while having a page as my authenticated root

I have an app that has devise set-up I would like to separate my marketing (landing pages) and application. Cannot figure out how to get unauthenticated users to be sent to my sessions#new login page(root). Once logged in the root page would be dashboard#pages.

I am using rails 5.2.2 and Devise 4.6.1

my routes.rb file

Rails.application.routes.draw do
    devise_scope :users do

        authenticated  do
            root to: 'dashboard#pages'
         end

        unauthenticated do
              root to: 'devise/sessions#new', as: 'unauthenticated_root'
        end

    end
end

This is the error I get.

Could not find devise mapping for path "/". This may happen for two reasons: 

1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end 

2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: @request.env["devise.mapping"] = Devise.mappings[:user]

1 Answer

I found the answer, here is my page.

  devise_scope :user do

    root 'devise/sessions#new' 
  end