How to structure authenticated routes when using Devise?

前端 未结 2 1576
庸人自扰
庸人自扰 2021-01-16 02:18

In my question How to have root view when user is not logged in rails? max answered that we can use authenticated to make routes available only when someone is

2条回答
  •  耶瑟儿~
    2021-01-16 02:52

    Here is the simple way to structure authenticated and unauthenticated routes.

    In app/controllers/application_controller.rb, add "before_action :authenticate_user!".

    My app/controllers/application_controller.rb file:

    class ApplicationController < ActionController::Base
    
    protect_from_forgery with: :exception
    
    before_action :authenticate_user!
    end
    

    My config/routes.rb:

    Rails.application.routes.draw do
      devise_for :users
      root "home#index"
      devise_for :users, controllers: {
                           :sessions => "users/sessions",
                           :registrations => "users/registrations" }
      authenticated :user do
          resources :students
      end
    
    
    
    unauthenticated :user do
      #Some route
      end
    
    end
    

提交回复
热议问题