简体   繁体   English

ActiveRecord :: RecordInvalid:验证失败:密钥不能为空

[英]ActiveRecord::RecordInvalid: Validation failed: Key can't be blank

After updating to Rails 4.0, I am getting this error. 更新到Rails 4.0后,出现此错误。

ActiveRecord::RecordInvalid (Validation failed: Key can't be blank):
  app/models/users_setting.rb:25:in `update_value'
  app/controllers/management_reports/employee_onboarding_controller.rb:35:in `update_filter_values'
  app/controllers/management_reports/employee_onboarding_controller.rb:57:in `prepare_to_read_data'
  app/controllers/management_reports/employee_onboarding_controller.rb:11:in `index'

This is the method: 这是方法:

def update_value options={}
  binding.pry
    self.update_attributes!({:value => options.inspect})
  end

pry shows me this: 撬给我看这个:

[5] pry(#<UsersSetting>)> self.update_attributes!({:value => options.inspect})
   (5.0ms)  BEGIN
   (5.0ms)  BEGIN
   (4.8ms)  ROLLBACK
   (4.8ms)  ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Key can't be blank
from /Users/justinhung/.rvm/gems/ruby-2.1.10/gems/activerecord-4.0.0/lib/active_record/validations.rb:57:in `save!'
[6] pry(#<UsersSetting>)> options
=> {:status=>"-1", :client_id=>"-100"}
[7] pry(#<UsersSetting>)> :value
=> :value
[8] pry(#<UsersSetting>)> value
=> "{:status=>\"-1\", :client_id=>\"-100\"}"
[9] pry(#<UsersSetting>)> :value => options.inspect
SyntaxError: unexpected =>, expecting end-of-input
:value => options.inspect
         ^
[9] pry(#<UsersSetting>)> options.inspect
=> "{:status=>\"-1\", :client_id=>\"-100\"}"
[10] pry(#<UsersSetting>)> options
=> {:status=>"-1", :client_id=>"-100"}

Not sure how to proceed, any help would be appreciated. 不确定如何进行,将不胜感激。

See options itself is a hash so you just need to pass options try, See options本身是一个哈希,因此您只需要传递options try,

self.update_attributes!(options)

where status and client_id are model attributes. 其中statusclient_id是模型属性。

Key can't blank and value can't blank are the rails validation error messages coming because of maybe you set presence true for these two fields. 由于可能将这两个字段的状态设置为true,因此会出现Rails验证错误消息,因此密钥不能为空,值不能为空。

If you want to skip validation you can do the following so that when you update rails don't shout for key and value presence. 如果要跳过验证,则可以执行以下操作,以便在更新rails时不要大喊关键和价值。

def update_value options={}
  self.status = options[:status]
  self.client_id = options[:client_id]
  self.save(validate: false)
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActiveRecord :: RecordInvalid(验证失败:Word不能为空) - ActiveRecord::RecordInvalid (Validation failed: Word can't be blank) ActiveRecord :: RecordInvalid:验证失败:通知者不能为空 - ActiveRecord::RecordInvalid: Validation failed: Notified by can't be blank 验证失败:电子邮件无效,密码不能为空(ActiveRecord :: RecordInvalid) - Validation failed: Email is invalid, Password can't be blank (ActiveRecord::RecordInvalid) Ruby on Rails Carrierwave:ActiveRecord :: RecordInvalid(验证失败:Avatar不能为空): - Ruby on Rails Carrierwave: ActiveRecord::RecordInvalid (Validation failed: Avatar can't be blank): ActiveRecord::RecordInvalid(验证失败:Uid 不能为空) Omniauth LinkedIn Devise - ActiveRecord::RecordInvalid (Validation failed: Uid can't be blank) Omniauth LinkedIn Devise OmniauthCallbacksController中的ActiveRecord :: RecordInvalid #tube验证失败:电子邮件不能为空 - ActiveRecord::RecordInvalid in OmniauthCallbacksController#twitter Validation failed: Email can't be blank ActiveRecord :: RecordInvalid:FactoryGirl验证失败 - ActiveRecord::RecordInvalid: Validation failed for FactoryGirl FactoryBot ActiveRecord :: RecordInvalid:验证失败:需要年份 - FactoryBot ActiveRecord::RecordInvalid: Validation failed: Year is required ActiveRecord::RecordInvalid:验证失败:用户必须存在 - ActiveRecord::RecordInvalid: Validation failed: Users must exist Rails 5.2.2.1 ActiveRecord :: RecordInvalid:验证失败 - Rails 5.2.2.1 ActiveRecord::RecordInvalid: Validation failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM