简体   繁体   中英

Devise Landing Page

I am using Rails 4 and wanting to have a landing page whereby the users can elect to either sign in or sign up. The issue I am having at present is that the routes keep directing me back to the sign_in.

Routes

Rails.application.routes.draw do

  get 'home/index'

  root 'home#index'

  devise_for :people

HomeController

class HomeController < ApplicationController
  skip_before_action :authenticate_user!

  def index
  end
end

PeopleController

class PeopleController < ApplicationController

  before_filter :set_person, only: [:show, :edit, :update, :destroy]
  before_filter :authenticate_person!

  respond_to :html

Terminal Output

Started GET "/" for 127.0.0.1 at 2015-11-17 22:28:51 +1100
  ActiveRecord::SchemaMigration Load (0.7ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by HomeController#index as HTML
Completed 401 Unauthorized in 20ms (ActiveRecord: 0.0ms)

Pro tip: Get rid of the before_action :authenticate_user! in your controllers, and use the following routes :

  #config/routes.rb
  authenticated :user do
    root 'home#dashboard', as: :root #-> if user is logged in
    resources :controller #-> ONLY available for logged in users
  end

  unauthenticated :user do
    root 'home#index', as: :unauthenticated #-> if user is not logged in
  end

If the user is authenticated, the authenticated routes will be presented, if not the unauthenticated routes will show.

The important thing to note is that authenticated :user means that those routes will only be available when the user is logged in. authenticate :user will allow the user to view the routes if not logged in, but will be redirected to the sign in page as you have currently.

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