[英]Direct user on sign-in in devise rails
成功使用devise登錄后,如何將用戶定向到特定頁面?
我已經安裝了devise,並且按照他們的規定,我已將root to: 'home#index'
添加root to: 'home#index'
,我將root to: 'home#index'
假定為呈現登錄的頁面。 我不知道的是,一旦用戶登錄,我如何redirect
用戶redirect
到一個已經存在的特定頁面,例如'graph/data'
。
如果只想重定向用戶一次,則可以覆蓋devise文檔中介紹的after_sign_in_path方法
## application_controller.rb
def after_sign_in_path_for(resource)
sign_in_url = new_user_session_url
if request.referer == sign_in_url
super
else
stored_location_for(resource) || request.referer || root_path
end
end
如果您希望用戶在登錄后可以重定向到特定路由,則可以使用devise_scope:
## routes.rb
devise_for :users
devise_scope :user do
authenticated :user do
root 'home#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
除了欺負者的答案:
在第二種情況下:
## routes.rb
devise_for :users
devise_scope :user do
authenticated :user do
root 'graphs#data', as: :authenticated_root
end
unauthenticated do
root 'home#index', as: :unauthenticated_root
end
end
但是對於這種情況,您可以將“ GraphsController”與操作“ data”一起使用。 另外,您應該使用':authenticate_user!'。 到這個。 例如:
class GraphsController < ApplicationController
before_filter :authenticate_user!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.