3. Set up the logged‐in root and create a status page - verachell/Simple-rails-tryout-app-using-devise GitHub Wiki

I'd like to configure it so that when we sign up or sign in, it takes you to a generic status page that is NOT the homepage. On the official devise docs, can set user root path in config/routes.rb (it otherwise defaults to regular root).

First though, create the page we want it to go to as the root. Create in the app/views/homepages folder, create a new file called status.html.erb with some basic info:

<h1>Sign in and out</h1>
<p>You should be redirected here after login or signup</p>

In config/routes.rb set user root path like this to connect user root to the status page we just made:

get 'status', to: 'homepages#status', as: :user_root

However - now hampered by inability to sign out! The DigitalOcean tutorial mentioned at the very start says to put this in whatever view you want (in this case app/views/homepages/status.html.erb)

<% if user_signed_in? %> 
 <div> Welcome <%= current_user.email %> </div> 
  <%= button_to "Sign out", destroy_user_session_path, method: :delete %> 
<% else %>
  <%= button_to "Sign in", new_user_session_path %> 
<% end %>

In addition, add the flash notices into status.html.erb:

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p> 

So app/views/homepages/status.html.erb should now look like this:

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p> 
<h1>Sign in and out</h1>
<p>You should be redirected here after login or signup</p>

<% if user_signed_in? %> 
 <div> Welcome <%= current_user.email %> </div> 
  <%= button_to "Sign out", destroy_user_session_path, method: :delete %> 
<% else %>
  <%= button_to "Sign in", new_user_session_path %> 
<% end %>

This works as intended so far - when a user signs in, the app automatically redirects them to this separate status page instead of the homepage. There is only one problem -for whatever reason, after sign out, it re-directs to home page (which we don't always want, would prefer the status page). The reason is probably that if signed out, we're not under "users" anymore. To fix, that, there is a whole page on that in the Devise wiki how-to at https://github.com/heartcombo/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

Important: see also https://github-wiki-see.page/m/heartcombo/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update

The issue is actually that after logout we don't want to redirect back to the same status page for reasons given in the link just above. Instead, the easiest thing to do is I need to create a logout page in homepages, and direct to that after logout.

Made a new file in app/views/homepages called logout.html.erb. Contents:

      <p class="notice"><%= notice %></p>
       <p class="alert"><%= alert %></p>
<h1>Log out</h1>
<p>You will arrive here after logout</p>

  <%= button_to "Sign in", new_user_session_path %> 

Finally, found a how-to explaining how to redirect to a different path after logout. Here https://github.com/heartcombo/devise/wiki/How-To:-Change-the-redirect-path-after-destroying-a-session-i.e.-signing-out

Need to add to the main application controller (this overloads the original method):

class ApplicationController < ActionController::Base
  private

  # Overwriting the sign_out redirect path method
  def after_sign_out_path_for(resource_or_scope)
    root_path
# replace root_path with whatever
  end
end

So did the equivalent for my app. At this point, my application controller looks like this:

class ApplicationController < ActionController::Base

private

  # VC Overwriting the sign_out redirect path method
  def after_sign_out_path_for(resource_or_scope)
    "/logout"
  end
end

Of course, need to make sure we already have a route for /logout in config/routes.rb:

get 'logout', to: 'homepages#logout'

We now have a page at localhost:3000/status where users are directed to after signing in. After logging out, they will be directed to localhost:3000/logout

⚠️ **GitHub.com Fallback** ⚠️