Skip to content

Devise Custom Redirections

Vijay Kurian edited this page Nov 7, 2019 · 1 revision

1 - This redirects a signed-up/logged-in user to the /posts route

in config/routes.rb add 

get 'posts' => 'posts#index', as: :user_root

This is a devise method which points a 'get' method to a default view, in this case posts/index.html.erb. This allows the application to redirect the user to the /posts upon successful Log-in/Sign-up

2 - Method to ensure only users signed in are able to see the /posts route

in app/controllers/posts_controller.rb add

before_action :signed_in
 private
    def signed_in
      redirect_to '/' if !user_signed_in?
    end

user_signed_in? is a default devise method which checks if a user is signed in, returns a boolean.