[英]Redirecting after sign in with Devise
我知道這個問題已被問過很多次,但不知怎的,它對我不起作用。 我希望用戶在登錄后重定向到特定頁面(使用Devise)。 我正在使用Rails 3.2和Ruby 4。
因為用戶被重定向到: - 登錄后,如果用戶從根路徑所屬的控制器控制的頁面之一到達登錄頁面,它們將被重定向到根目錄。 這些頁面不需要授權。 - 登錄后,如果用戶通過單擊需要授權的頁面自動進入登錄頁面,他們將被重定向到所請求的頁面。
要在登錄到特定頁面后重定向用戶,我了解您需要向控制器添加一些行。 對於控制我添加的所需授權頁面的控制器:
class AccountsController < ApplicationController
before_filter :authenticate_user!
def user1_home
end
def user2_home
end
def user3_home
end
protected
def after_sign_in_path_for(resource)
'accounts/user1_home'
end
end
但是,這並沒有改變任何事情。 我也嘗試使用def after_sign_in_path_for(resource) 'accounts#user1_home_path' end
我是否也應該改變routes.rb中的內容? 我的路線是:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
toc GET /toc(.:format) pages#toc
stakeholder GET /stakeholder(.:format) pages#stakeholder
about GET /about(.:format) pages#about
doelgroep_onderneming GET /doelgroep_onderneming(.:format) pages#doelgroep_onderneming
doelgroep_ngo GET /doelgroep_ngo(.:format) pages#doelgroep_ngo
doelgroep_se GET /doelgroep_se(.:format) pages#doelgroep_se
partnership GET /partnership(.:format) pages#partnership
contact GET /contact(.:format) pages#contact
account_stakeholder GET /account/stakeholder(.:format) accounts#stakeholder_home
account_business GET /account/business(.:format) accounts#business_home
account_admin GET /account/admin(.:format) accounts#admin_home
root / pages#index
您絕對不應該在Accounts控制器中定義每個用戶的方法。
Devise在github上有完成此操作的說明: https : //github.com/plataformatec/devise/wiki/How-To :-redirect-to-a-specific-page-on-successful-sign-in
你需要將after_sign_in_path_for移動到ApplicationController類,你不應該有'accounts/user1_home'
。 要為每次使用找到正確的URL,你應該有一些user_path(@user)的內容,但是我沒有足夠的代碼來確切知道要使用什么。 您應該在控制台窗口中運行rake routes
命令,以查看您提供的路由。 有關路由的更多信息,請訪問: http : //guides.rubyonrails.org/routing.html
是。 在任何時候(登錄之前或之后)設置以下內容:
session["user_return_to"] = request.original_url # Will redirect the user to where they came from
如果它是一個經過身份驗證的頁面,他們在登錄之前嘗試訪問您想要重定向到那么您可以在ApplicationController中以這種方式執行:
def after_sign_in_path_for(resource)
if session[:user_return_to] == nil
your_actual_next_path
else
super
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.