簡體   English   中英

Rails + Devise:重定向到用戶資源

[英]Rails + Devise: Redirect to User resource

當我的application_controller.rb重定向到users_path如下所示:

def after_sign_in_path_for(resource)
    users_path
end

重定向工作。 但是,如果我將其更改為user_path

def after_sign_in_path_for(resource)
    user_path
end

我收到路由錯誤。 這是我的routes.rb:

devise_for :users
get "users/show"

resources :orders

resources :users do
    resources :orders
end

我想也許我應該像這樣傳遞userID:

def after_sign_in_path_for(resource)
    @user = User.find(params[:id])
    user_path(@user)
end

但錯誤返回Couldn't find User without an ID 救命?

我想你應該在登錄后嘗試使用current_user

user_path(current_user)

我花了幾個小時試圖獲得相同的功能,這是ApplicationController中最終為我工作的代碼:

def after_sign_in_path_for(resource)
    current_user
end

如果我曾經嘗試過current_user_path ,我總是得到undefined local variable or method current_user_path錯誤。

另外,我使用的是Rails 3.2.8和Devise 2.1.2。

希望有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM