【问题标题】:Devise Landing Page设计登陆页面
【发布时间】:2015-11-17 11:36:36
【问题描述】:

我正在使用 Rails 4 并希望有一个登录页面,用户可以选择登录或注册。我目前遇到的问题是路线不断将我引导回登录。

路线

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)

【问题讨论】:

    标签: ruby-on-rails ruby devise routes


    【解决方案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 页面。

    【讨论】:

    • 所以我已经实现了上述操作,它似乎正在向我显示一个登录页面,但如果我在登录之前导航到一个页面,它会告诉我该路线不存在
    • devise_for :people 认证 :user do root 'home#dashboard', as: :root #-> 如果用户已登录 #-> 仅可用于登录用户资源 :people 资源 :venues 资源:store_configs 资源 :orders 资源 :menu_items end unauthenticated :user do root 'home#index', as: :unauthenticated #-> if user is not logged in end
    猜你喜欢
    • 2016-04-28
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多