Ruby-on-rails 尝试更新数据库值时,rails update_attributes 返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10189139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:14:07  来源:igfitidea点击:

rails update_attributes returns false when trying to update db values

ruby-on-railsrubyruby-on-rails-3controllerupdate-attributes

提问by steve

hoping someone here can point me in the right direction.

希望这里有人能指出我正确的方向。

I have a controller Update def running "update_attributes". Currently it returns false, with no error message. I'm fairly new to Ruby, but not to coding, and this has had me stumped for a good few days! I am trying to get the User model and db updated with the values specified below.

我有一个运行“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

my edit def....

我的编辑定义....

def edit
    @user = current_user
end

The form sends the following to this update method:

该表单将以下内容发送到此更新方法:

--- !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'

And params[:user] returns:

params[:user] 返回:

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

All these fields are in attr_accessible in the Model, and I can create a user without any problem.

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

here's development.log output (sorry it's a bit messy)....

这是 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"=>"[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)

Could anyone possibly help point out where I'm going wrong, please?

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

Thanks in advance!

提前致谢!

回答by yfeldblum

Both #update_attributesand #savewill first check if your model instance is #valid?before continuing on to write to the database. If your model instance is not #valid?, then these methods will return false. To see what's wrong with your model instance, take a look at your model's #errors.

双方#update_attributes#save会先检查你的模型实例是#valid?继续到写入到数据库之前。如果您的模型实例不是#valid?,那么这些方法将返回 false。要查看模型实例有什么问题,请查看模型的#errors.

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

Or, embedded in your method,

或者,嵌入到您的方法中,

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

回答by Galen

You can also get better introspection when updating attributes by calling update_attributes!instead, which raises exceptions instead of just returning false.

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