铁轨 +设计:重定向到用户资源
当我的 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
我想也许我应该像这样传递用户ID:
def after_sign_in_path_for(resource)
@user = User.find(params[:id])
user_path(@user)
end
但错误返回无法找到没有ID的用户
。帮助?
When my application_controller.rb
redirects to users_path
like this:
def after_sign_in_path_for(resource)
users_path
end
the redirect works. But if I change it to user_path
:
def after_sign_in_path_for(resource)
user_path
end
I get a routing error. Here is my routes.rb:
devise_for :users
get "users/show"
resources :orders
resources :users do
resources :orders
end
I thought maybe I should pass the userID like this:
def after_sign_in_path_for(resource)
@user = User.find(params[:id])
user_path(@user)
end
but the error comes back Couldn't find User without an ID
. Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该在登录后尝试
current_user
I think you should try
current_user
after login我花了几个小时试图获得相同的功能,这是 ApplicationController 中最终为我工作的代码:
如果我尝试过 current_user_path,我总是得到未定义的局部变量或方法current_user_path 错误。
另外,我正在使用 Rails 3.2.8 和 Devise 2.1.2。
希望有帮助。
I spent several hours trying to get the same functionality, and this is the code in the ApplicationController that ended up working for me:
If I ever tried
current_user_path
, I always gotundefined local variable or method current_user_path
errors.Also, I'm using Rails 3.2.8 and Devise 2.1.2.
Hope that helps.