简体   繁体   中英

Rails routes for a static landing page if user not logged in

In my application I am using the Devise authentication gem which works really well. I also have built a static landing page which I currently have in the /public directory.

I can of course browser to localhost:3000/landing and see the page (as per the route below) but what I'm trying to achieve but cannot seem to figure out is how to setup my routes.rb file so that the landing.html file is the root, but when a user is logged in, their root becomes companies#index .

Here is my routes.rb file

Rails.application.routes.draw do
  
  devise_for :users
  get 'dashboard/index'
  
  get '/landing', :to => redirect('/landing.html')
  
  root 'companies#index'
  
  resources :companies do
    resources :shareholders
    resources :captables do
      post :subscribe_to_captable
      resources :events do
        post :lock_event
        post :unlock_event
        resources :transactions
      end
    end
  end
end

Here is my application_controller.rb

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

One way is to put the redirect inside your CompaniesController.rb index method do this.

CompanesController.rb (or whatever it is called)

def index
  unless user_signed_in?
    redirect_to landing_pages_path (or whatever the name of the route is)
  end
end

Then change the route in the routes.rb file.

get '/landing', to: 'companies#landing' * change companies here to the name of the controller holding the landing page's method if it is not in the companies controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM