Mysql 更新属性尝试更新属性时失败

Mysql 更新属性尝试更新属性时失败,mysql,ruby-on-rails,Mysql,Ruby On Rails,我有一个用户数据库,Userid:integer,username:string,创建时间:datetime,更新时间:datetime,密码摘要:string,admin:boolean,usermanager:boolean。 我想更新一个用户并使其成为Admin,将Admin变量设置为true 控制器中的我的更新: def update @user = User.find(params[:id]) if @user.update_attributes(user_params) flas

我有一个用户数据库,Userid:integer,username:string,创建时间:datetime,更新时间:datetime,密码摘要: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_属性在所有情况下都失败。我打开了一个调试器,试图调用update_属性,但也失败了

(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_属性也适用于相同的情况

(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)

知道我在这里遗漏了什么吗?

也许我认为当前的用户数据是无效的。请检查user.errors

范例

user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,:confirmable,
         :recoverable, :rememberable, :trackable, :validatable, :lockable

  validates :name, presence: true
end
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用户参数参数。要求:用户。允许:用户名,:密码,:密码确认,:管理员结束。是否有任何验证?是否有多个:时区,相关::销毁有效的用户名\u REGEX=/\A[A-zA-Z0-9]+\z/验证:用户名,状态:true,长度:{minimum:6,maximum:15},格式:{with:VALID\u USERNAME\u REGEX},唯一性:true验证:密码,存在性:true,长度:{minimum:6,maximum:15}有\u secure\u密码不用于:admin字段如果您只更新admin字段,请尝试@user.update\u列:admin,true。检查它是否有效。是的,似乎是这样。但是,我正在使用bcrypt,我的密码将为空,并且将创建密码摘要。使用update_列是解决这个问题的方法吗?[不能为空,太短,最小为6个字符]}>可能我认为您需要在更新用户数据时删除passward参数。我写了补充答案。
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