设计登陆页面

时间:2015-11-17 11:36:37

标签: ruby-on-rails ruby devise routes

我正在使用Rails 4并希望拥有一个目标网页,用户可以选择登录或注册。我目前遇到的问题是路线一直指示我回到sign_in。

路线

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

终端输出

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)

1 个答案:

答案 0 :(得分:1)

专业提示:摆脱控制器中的before_action :authenticate_user!,并使用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

如果用户已通过身份验证,则会显示authenticated路由,否则将显示unauthenticated路由。

需要注意的重要一点是,authenticated :user表示当用户登录时, 这些路由可用。authenticate :user将允许用户查看路由如果没有登录,但会被重定向到您当前的sign in页面。