繁体   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