在我的應用程序中,我有一個用戶確認過程。當用戶註冊四件事情發生:Ruby on Rails用戶確認不更新參數
- 的account_status_id設置爲1(未經證實)
- 該用戶是在(現在CURRENT_USER存在)簽署
- 生成一個new_account_confirmation_token
- 確認電子郵件通過鏈接發送給新用戶,其中包含new_account_confirmation_token
我最初嘗試使用此方法處理確認鏈接。它發現用戶沒有問題,代碼流經update_attributes!方法,但它不更新account_status。從我可以告訴它是由於current_user對象存在的事實,因此用戶Im試圖更新已經「在內存中」。那是對的嗎?
def new_account_confirmation
@title = "Account Confirmation"
logger.info("User is not logged in")
@user = User.find_by_new_account_confirmation_token(params[:confirm_id])
if @user
@user.update_attributes!(:account_status_id => 2)
else
redirect_to root_path
end
end
我的工作如下。下面的代碼工作,但我想知道爲什麼上面的代碼不起作用。爲什麼不更新account_status?
def new_account_confirmation
@title = "Account Confirmation"
if current_user
logger.info("User is logged in")
current_user.update_attributes!(:account_status_id => 2)
else
logger.info("User is not logged in")
@user = User.find_by_new_account_confirmation_token(params[:confirm_id])
if @user
@user.update_attributes!(:account_status_id => 2)
else
redirect_to root_path
end
end
end
您可以確認第一組代碼是否更新數據庫中的行?它看起來正確。但是,是的,我想象因爲current_user在內存中,所以它不會更新。出於好奇,你在會話中序列化整個用戶對象嗎?一般來說,最好只存儲該ID,並根據請求進行查詢,以便始終獲得最新的信息。 – agmcleod
第一種方法不會更新數據庫中的行。我只在會話中存儲user_id,而不是整個current_user對象 – hoitomt