我有一个用户数据库作为User(id:integer,username:string,created_at:datetime,updated_at:datetime,password_digest:string,admin:boolean,usermanager:boolean)。 我想更新用户并让他成为Admin,将admin变量设置为true。
我在控制器中的更新:
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "User edited"
redirect_to current_user
else
render users_url
end
end
update_attributes在所有情况下都失败了。我打开了一个调试器并尝试调用update_attributes,它也失败了。
(byebug) user_params = {"admin"=>true}
{"admin"=>true}
(byebug) @user
#<User id: 1, username: "dsmegha", created_at: "2015-09-11 07:42:01", updated_at: "2015-09-13 03:25:01", password_digest: "$2a$10$BInH6J1dXHanqNIdGag6megSyrmm95AmjTEGPemNdGU...", admin: false, usermanager: nil>
(byebug) @user.update_attributes(user_params)
(0.2ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE ("users"."username" = 'dsmegha' AND "users"."id" != 1) LIMIT 1
(0.2ms) rollback transaction
false
update_attribute适用于同一个。
(byebug) @user.update_attribute(:admin,true)
(0.2ms) begin transaction
(0.1ms) commit transaction
true
(byebug) @user
#<User id: 1, username: "dsmegha", created_at: "2015-09-11 07:42:01", updated_at: "2015-09-13 03:25:01", password_digest: "$2a$10$BInH6J1dXHanqNIdGag6megSyrmm95AmjTEGPemNdGU...", admin: true, usermanager: nil>
(byebug)
知道我在这里缺少什么吗?
答案 0 :(得分:0)
也许我认为当前的用户数据无效。请查看user.errors
。
例)
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,:confirmable,
:recoverable, :rememberable, :trackable, :validatable, :lockable
validates :name, presence: true
end
撬
pry(main)> user = User.last
=> #<User:0x007fe328fe56d8
id: 3,
email: "hogehoge@example.com",
name: "",
encrypted_password: "$2a$10$I.xXo0AtAz8nuCUzOGclPe6PA//XMZtjW6fh94D1t25jb7eV9bsS.",
current_sign_in_at: Tue, 02 Jun 2015 01:53:08 JST +09:00,
last_sign_in_at: Tue, 02 Jun 2015 01:52:20 JST +09:00,
current_sign_in_ip: "127.0.0.1",
last_sign_in_ip: "127.0.0.1",
created_at: Tue, 02 Jun 2015 01:49:40 JST +09:00,
updated_at: Sun, 13 Sep 2015 13:16:26 JST +09:00,
confirmation_token: nil,
confirmed_at: Tue, 02 Jun 2015 01:52:01 JST +09:00,
confirmation_sent_at: Tue, 02 Jun 2015 01:49:40 JST +09:00,
unconfirmed_email: nil,
failed_attempts: 0,
unlock_token: nil,
locked_at: nil,
status: 1>
[7] pry(main)> user.update_attributes(status: 2)
(0.1ms)
BEGIN
(0.2ms)
ROLLBACK
=> false
[8] pry(main)> user.errors
=> #<ActiveModel::Errors:0x007fe328d893f8
@base=
#<User:0x007fe328fe56d8
id: 3,
email: "hogehoge@example.com",
name: "",
encrypted_password: "$2a$10$I.xXo0AtAz8nuCUzOGclPe6PA//XMZtjW6fh94D1t25jb7eV9bsS.",
reset_password_token: "XWmjsEBujb2-aWd5YHYT",
reset_password_sent_at: nil,
remember_created_at: nil,
sign_in_count: 2,
current_sign_in_at: Tue, 02 Jun 2015 01:53:08 JST +09:00,
last_sign_in_at: Tue, 02 Jun 2015 01:52:20 JST +09:00,
current_sign_in_ip: "127.0.0.1",
last_sign_in_ip: "127.0.0.1",
created_at: Tue, 02 Jun 2015 01:49:40 JST +09:00,
updated_at: Sun, 13 Sep 2015 13:16:26 JST +09:00,
confirmation_token: nil,
confirmed_at: Tue, 02 Jun 2015 01:52:01 JST +09:00,
confirmation_sent_at: Tue, 02 Jun 2015 01:49:40 JST +09:00,
unconfirmed_email: nil,
failed_attempts: 0,
unlock_token: nil,
locked_at: nil,
status: 2>,
@messages={:name=>["can't be blank"]}>
另外写。
查看是否有密码输入字段?在这种情况下,我认为您需要在更新用户数据时删除passward参数。
这个怎么样?
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,:confirmable,
:recoverable, :rememberable, :trackable, :validatable, :lockable
validates :name, presence: true
def update_without_current_password(params, *options)
params.delete(:current_password)
if params[:password].blank?
params.delete(:password)
params.delete(:password_confirmation) if params[:password_confirmation].blank?
end
clean_up_passwords
update_attributes(params, *options)
end
end
控制器
def update
@user = User.find(params[:id])
if @user.update_without_current_password(user_params)
flash[:success] = "User edited"
redirect_to current_user
else
render users_url
end
end