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更新属性时获得更好的反省。

相关问题