Im having problems understanding the app logic to this password reset code i found below on the web.
- A user receives a an email with a link with some reset code.
- After clicking on this they go to the action below called reset
- The user is found in the db by referencing the reset code.
- The form to change password is shown and the user enters the new password in.
Heres where i get confused.
- When the form is submitted the action is called again.
- This time there will be no reset code in the params so no user will be found @user = nil
- This time its a post request so we enter that part of the logic.
My question is - How can this code ever be valid if @user = nil if @user.update_attributes(:password => params[:user][:password], :password_confirmation => params[:user][:password_confirmation])
# app/controllers/users_controller.rb
def reset
@user = User.find_by_reset_code(params[:reset_code]) unless params[:reset_code].nil?
if request.post?
if @user.update_attributes(:password => params[:user][:password], :password_confirmation => params[:user][:password_confirmation])
self.current_user = @user
@user.delete_reset_code
flash[:notice] = "Password reset successfully for #{@user.email}"
redirect_to root_url
else
render :action => :reset
end
end
end
# app/views/users/reset.html.erb
<%= error_messages_for :user %>
<% form_for :user do |f| -%>
Pick a new password for <%= @user.email %>
Password
<%= f.password_field :password %>
Confirm Password
<%= f.password_field :password_confirmation %>
<%= submit_tag 'Reset' %>
<% end -%>