简体   繁体   English

设计登陆页面

[英]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. 我正在使用Rails 4,并希望有一个登录页面,用户可以选择登录或注册。 The issue I am having at present is that the routes keep directing me back to the sign_in. 我目前遇到的问题是路线不断引导我回到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! 专业提示:摆脱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. 如果用户通过了身份验证,则将显示已通过authenticated路由,否则,将显示unauthenticated authenticated unauthenticated路由。

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. 需要注意的重要一点是, authenticated :user表示当用户登录时这些路由才可authenticate :user将允许用户查看路由(如果未登录),但将重定向到sign in页面。如您目前的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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