[英]Route user to users#show after devise password change
When my user logs in, I would like them to default to their own "show" page as I don't want a listing of users viewable. 当我的用户登录时,我希望他们默认使用自己的“显示”页面,因为我不希望看到用户列表。 So to do this, I have changed the default after_sign_in, like this;
为此,我更改了默认的after_sign_in,如下所示:
def after_sign_in_path_for(resource)
user_path(current_user)
end
Works great. 效果很好。 It goes wrong after a password change.
更改密码后出错。 I need to modify the root path so that after a change, it goes to the show page
我需要修改根路径,以便在更改后进入显示页面
root :to => "users#show"
..but now, obviously, I don't have an :id, so I get ..但是现在,显然,我没有:id,所以我得到了
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User without an ID
...which makes sense. ...很有意义。 I am using the controllers which ship with devise, so my question is, how do I hook the password change and send the user to their show page?
我正在使用devise随附的控制器,所以我的问题是,如何挂钩密码更改并将用户发送到其显示页面? Should I be able to just edit the def show to say
我应该能够编辑def show说
def show
@user = User.find(params[:id]) || current_user
or do I need to modify my 还是我需要修改我的
root :to => "users#show"
to reflect the id of the current_user, assuming devise logged the user back in automatically.... or did it never log them out...? 以反映current_user的ID,假设devise自动将用户重新登录...。还是从未注销过用户...?
Thanks 谢谢
You do it like this; 你是这样做的;
First up, update your routes file to use the status of Devise so authenticated users are treated differently 首先,更新您的路由文件以使用Devise的状态,以便对身份验证的用户进行不同的处理
authenticated :user do
root :to => "users#show"
end
unauthenticated :user do
devise_scope :user do
get "/" => "devise/sessions#new"
end
end
Now make sure that when user#show happens, current_user is looked up and used to combat the fact that the users#show has no :id 现在确保在发生user#show时,将查找current_user并用于解决users#show没有:id的事实
def show
if params[:id].nil? # if there is no user id in params, show current one
@user = current_user
else
@user = User.find(params[:id])
end
Its all good! 都很好!
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.