iamspauljamez iamspauljamez - 5 months ago 57
JSON Question

Rails edit serialized JSON data

I have a column that stores JSON data. I don't know how to show it when it is on Edit state.

serialize :value, JSON

= f.fields_for :value do |ff|
.form-group
= ff.label :short
= ff.text_field :short, class: 'form-control'
.form-group
= ff.label :long
= ff.text_field :long, class: 'form-control'

Answer

In place of

= f.fields_for :value do |ff|

please use the following code:

= f.fields_for :value, OpenStruct.new(@object.value) do |ff|

You will need to replace @object with your model object.