How to edit a Rails serialized field in a form?

后端 未结 8 717
-上瘾入骨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条回答
  •  猫巷女王i
    2020-12-04 08:03

    No need setter/getters, I just defined in the model:

    serialize :content_hash, Hash
    

    Then in the view, I do (with simple_form, but similar with vanilla Rails):

      = f.simple_fields_for :content_hash do |chf|
        - @model_instance.content_hash.each_pair do |k,v|
          =chf.input k.to_sym, :as => :string, :input_html => {:value => v}
    

    My last issue is how to let the user add a new key/value pair.

提交回复
热议问题