希望这里有人能指出我正确的方向。当试图更新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)
谁能可能会帮助指出我要去哪里错了吗?
在此先感谢!
你是否检查过'development.log'? – Alex 2012-04-17 10:06:19
错误消息的描述会很好 - 不知道你是如何得到'false' – Jonathan 2012-04-17 10:08:44
在主文章中增加了development.log输出....(刚刚回滚) – steve 2012-04-17 10:11:48