How to edit a Rails serialized field in a form?

后端 未结 8 692
-上瘾入骨i
-上瘾入骨i 2020-12-04 07:16

I have a data model in my Rails project that has a serialized field:

class Widget < ActiveRecord::Base
  serialize :options
end

The opti

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 07:46

    I will suggest something simple, because all the time, when user will save form You will get string. So You can use for example before filter and parse those data like that:

    before_save do
      widget.options = YAML.parse(widget.options).to_ruby
    end 
    

    of course You should add validation if this is correct YAML. But it should works.

提交回复
热议问题