[英]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. 其中status和client_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.