Rails + Devise:重定向到用户资源

时间:2011-06-20 00:30:32

标签: ruby-on-rails devise

当我的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。帮助

2 个答案:

答案 0 :(得分:7)

我认为您应该在登录后尝试current_user

user_path(current_user)

答案 1 :(得分:1)

我花了几个小时试图获得相同的功能,这是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。

希望有所帮助。