【问题标题】:rails update_attributes returns false when trying to update db values尝试更新数据库值时,rails update_attributes 返回 false
【发布时间】:2012-04-17 10:04:50
【问题描述】:

希望这里有人可以为我指明正确的方向。

我有一个控制器 Update 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 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: example@googlemail.com
commit: Update
action: update
controller: users
id: '20'

params[:user] 返回:

{"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"example@googlemail.com"}

所有这些字段都在Model中的attr_accessible中,我可以毫无问题地创建用户。

这里是 development.log 输出(抱歉有点乱)......

Started PUT "/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"=>"example@googlemail.com"},     "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"=>"example@googlemail.com"}
     [1m[35m (0.0ms)[0m  begin transaction
     [1m[36mUser Exists (0.0ms)[0m  [1mSELECT 1 FROM "users" WHERE (LOWER("users"."email") =     LOWER('example@googlemail.com') AND "users"."id" != 20) LIMIT 1[0m
  [1m[35mUser Exists (0.0ms)[0m  SELECT 1 FROM "users" WHERE ("users"."email" = 'example@googlemail.com'     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)

谁能帮忙指出我哪里出错了?

提前致谢!

【问题讨论】:

  • 你检查过development.log吗?
  • 对错误消息的描述会很好 - 不知道你是怎么得到false
  • 在主帖中添加了 development.log 输出....(只是得到回滚)

标签: ruby-on-rails ruby ruby-on-rails-3 controller update-attributes


【解决方案1】:

#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

【讨论】:

  • 就是这样!这正是我所需要的!错误日志显示我缺少一个需要与这些一起更新的字段。非常感谢,现在我也知道如何正确调试这些错误了! :-)
  • 我现在面临类似的问题,我尝试了 Rails.logger.info(@user.errors.messages.inspect) 并返回 "True" 。这是什么意思?我必须更新一列,并且已在 attr_accessible 中添加了该列。在更新它时返回错误并且正在执行的查询也很奇怪。看看这里paste.lisp.org/+2S4Y 我无法更新这些字段。可能是什么问题?
  • @JyotiRanjan check @user.errors.messages.inspect 它至少在我的 Rails 4.2 中有效
【解决方案2】:

您还可以在更新属性时通过调用 update_attributes! 来获得更好的自省,这会引发异常,而不仅仅是返回 false。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    相关资源
    最近更新 更多