我正在使用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)
答案 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
页面。