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;
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
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? Should I be able to just edit the def show to say
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...?
Thanks
You do it like this;
First up, update your routes file to use the status of Devise so authenticated users are treated differently
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.