dp1
具有三个可能的状态true
,false
和null
。我想将dp1
另存为null
。根据日志正确完成了此操作,但是当我的数据库中没有默认值时,它将另存为true
。
Started PATCH "/equipments/2" for 127.0.0.1 at 2018-08-01 11:49:23 +0200
Processing by EquipmentsController#update as JS
Parameters: {"utf8"=>"✓", "equipment"=>{"title"=>"TV ", "dp1"=>"nil"}, "commit"=>"Save", "id"=>"2"}
Equipment Load (0.1ms) SELECT "equipment".* FROM "equipment" WHERE "equipment"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
↳ app/controllers/equipments_controller.rb:124
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 3], ["LIMIT", 1]]
↳ /Users/Mesnet/.rvm/gems/ruby-2.5.1/gems/orm_adapter-0.5.0/lib/orm_adapter/adapters/active_record.rb:17
(0.0ms) begin transaction
↳ app/controllers/equipments_controller.rb:81
Equipment Update (0.4ms) UPDATE "equipment" SET "dp1" = ?, "updated_at" = ? WHERE "equipment"."id" = ? [["dp1", 1], ["updated_at", "2018-08-01 09:49:23.413563"], ["id", 2]]
↳ app/controllers/equipments_controller.rb:81
(0.7ms) commit transaction
↳ app/controllers/equipments_controller.rb:81
Rendering equipments/js/create.js.erb
Rendered equipments/js/create.js.erb (0.4ms)
Completed 200 OK in 31ms (Views: 26.0ms | ActiveRecord: 1.5ms)
如果表单为null
,如何强制我的值另存为null
?
答案 0 :(得分:0)
仅空字符串或nil
值被强制为nil
。请检查boolean type documentation的有效记录。
您将必须检查'nil'字符串,然后在控制器级别将其转换为空字符串或nil值。像这样:
def update
.....
data = params.require(: equipment).permit(:title, :dp1).to_h
data[:dp1] = '' if data[:dp1] == 'nil'
.....
end