我有問題的理解應用程序邏輯到這個密碼重置代碼,我發現下面的網絡。RAILS - 不理解密碼重置代碼
- 用戶收到一封帶有 鏈接的電子郵件,其中包含一些重置代碼。
- 點擊之後,他們去 以下稱爲動作復位
- 用戶在數據庫中發現 引用重置密碼。
- 改變密碼的形式示出 並且用戶在進入新密碼 。
赫雷什其中i混淆。
- 當表單提交後,再次調用該操作。
- 這次在params中將沒有重置代碼,所以沒有用戶將被發現@user = nil
- 這次它發佈了一個請求,所以我們輸入了這部分邏輯。
我的問題是 - 如何可以將此代碼永遠是有效的,如果@user =零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| -%>
<p>
Pick a new password for <span><%= @user.email %></span>
</p>
<p>
<label for="password">Password</label><br />
<%= f.password_field :password %>
</p>
<p>
<label for="password">Confirm Password</label><br />
<%= f.password_field :password_confirmation %>
</p>
<p>
<%= submit_tag 'Reset' %>
</p>
<% end -%>
感謝您對錯誤的代碼,我糾正它。那麼表單提交回重置操作後會發生什麼。 @user將是零。但是在request.post中使用?阻止更新用戶。我不知道如何更新用戶? – robodisco 2010-01-06 04:58:12