15

希望這裏有人能指出我正確的方向。當試圖更新db值時,rails update_attributes返回false

我有一個控制器更新def運行「update_attributes」。目前它返回false,沒有錯誤信息。我對Ruby相當陌生,但對編碼不太熟悉,這讓我難以忍受好幾天!我正在嘗試使用下面指定的值更新用戶模型和數據庫。

def update 
    #get currently logged in user 
    @user = current_user 
    #update user params based on edit form... 
    if @user.update_attributes(params[:user]) 
     redirect_to profile_path, :notice => "Successfully updated profile." 
    else 
     render :action => 'edit' 
    end 
end 

我的編輯DEF ....

def edit 
    @user = current_user 
end 

形式發送以下此更新方法:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
utf8: ✓ 
_method: put 
authenticity_token: 9T4ihVI0p8j7pZEFxof3Bfahi2a+o3BPmtXJDnfHT4o= 
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
    first_name: Amanda 
    last_name: Ross 
    is_owner: '1' 
    email: [email protected] 
commit: Update 
action: update 
controller: users 
id: '20' 

而params [:用戶]返回:

{"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"[email protected]"} 

所有這些字段都在attr_accessible中模型,我可以創建一個沒有任何問題的用戶。

這裏的development.log輸出(抱歉,這是一個有點亂)....

Started PUT "https://stackoverflow.com/users/20" for 127.0.0.1 at 2012-04-17 10:39:29 +0100 
Processing by UsersController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"9T4ihVI0p8j7pZEFxof3Bfahi2a+o3BPmtXJDnfHT4o=", "user"=> {"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"[email protected]"},  "commit"=>"Update", "id"=>"20"} 
    [1m[36mUser Load (1.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 20 LIMIT 1[0m 
>>>>>>>>>>>> 
{"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"[email protected]"} 
    [1m[35m (0.0ms)[0m begin transaction 
    [1m[36mUser Exists (0.0ms)[0m [1mSELECT 1 FROM "users" WHERE (LOWER("users"."email") =  LOWER('[email protected]') AND "users"."id" != 20) LIMIT 1[0m 
    [1m[35mUser Exists (0.0ms)[0m SELECT 1 FROM "users" WHERE ("users"."email" = '[email protected]'  AND "users"."id" != 20) LIMIT 1 
    [1m[36m (0.0ms)[0m [1mrollback transaction[0m 
Rendered users/_form.html.erb (7.0ms) 
Rendered users/edit.html.erb within layouts/application (10.0ms) 
Rendered layouts/_includes.html.erb (30.0ms) 

誰能可能會幫助指出我要去哪裏錯了嗎?

在此先感謝!

+0

你是否檢查過'development.log'? – Alex 2012-04-17 10:06:19

+0

錯誤消息的描述會很好 - 不知道你是如何得到'false' – Jonathan 2012-04-17 10:08:44

+0

在主文章中增加了development.log輸出....(剛剛回滾) – steve 2012-04-17 10:11:48

回答

39

在繼續寫入數據庫之前,#update_attributes#save都將首先檢查您的模型實例是否爲#valid?。如果您的模型實例不是#valid?,那麼這些方法將返回false。要查看模型實例出了什麼問題,請查看模型的#errors

Rails.logger.info(@user.errors.messages.inspect) 

或者嵌入在你的方法,

def update 
    @user = current_user 
    if @user.update_attributes(params[:user]) 
    redirect_to profile_path, :notice => "Successfully updated profile." 
    else 
    # print the errors to the development log 
    Rails.logger.info(@user.errors.messages.inspect) 
    render :action => 'edit' 
    end 
end 
+0

就是這樣!這正是我需要的!錯誤日誌顯示我錯過了需要隨這些更新的字段。非常感謝,現在我知道如何正確調試這些錯誤! :-) – steve 2012-04-17 10:21:03

+1

我現在面臨類似的問題,我試過Rails.logger.info(@ user.errors.messages.inspect) ,它返回「True」。這是什麼意思? 我必須更新一列,並且我在attr_accessible中添加了列。同時更新它返回false,並查詢它正在執行也很奇怪。看看這裏 http://paste.lisp.org/+2S4Y 我不能更新字段。可能是什麼問題? – 2012-06-01 21:16:13

+0

@JyotiRanjan檢查'@ user.errors.messages.inspect'它至少在我的情況下使用Rails 4.2 – jmarceli 2015-07-07 09:38:52

4

您也可以通過調用update_attributes!代替,這引起了異常,而不是剛剛返回false更新屬性時獲得更好的反省。

相關問題