簡體   English   中英

設計登陸頁面

[英]Devise Landing Page

我正在使用Rails 4,並希望有一個登錄頁面,用戶可以選擇登錄或注冊。 我目前遇到的問題是路線不斷引導我回到sign_in。

路線

Rails.application.routes.draw do

  get 'home/index'

  root 'home#index'

  devise_for :people

家庭控制器

class HomeController < ApplicationController
  skip_before_action :authenticate_user!

  def index
  end
end

人員控制器

class PeopleController < ApplicationController

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

  respond_to :html

終端輸出

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)

專業提示:擺脫before_action :authenticate_user! 在您的控制器中,並使用以下路線

  #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

如果用戶通過了身份驗證,則將顯示已通過authenticated路由,否則,將顯示unauthenticated authenticated unauthenticated路由。

需要注意的重要一點是, authenticated :user表示當用戶登錄時這些路由才可authenticate :user將允許用戶查看路由(如果未登錄),但將重定向到sign in頁面。如您目前的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM