I am exploring dynamic forms for my app and currently I would like to build a nested hash and currently following Railscasts' ep 403. Thus, the following are my model and form:
Model:
class Product < ActiveRecord::Base
belongs_to :product_type
serialize :properties, Hash
end
Form:
<%= f.hidden_field :product_type_id %>
<%= f.fields_for :properties, OpenStruct.new(@product.properties) do |builder| %>
<% @product.product_type.fields.each do |field| %>
<%= render "products/fields/#{field.field_type}", field: field, f: builder %>
<% end %>
<% end %>
The output I am getting is:
"properties"=>{"Name"=>"Great Prod", "Remarks"=>"Super!", "Year"=>"2015"}
Data structure view:
- properties
-- Name: "Great Prod"
-- Remarks: "Super!"
-- Year: "2015"
What I am looking for is:
"properties"=>{"2015"=>{"Name"=>"Great Prod", "Remarks"=>"Super!"}}
Data structure view:
- properties
-- "2015"
--- Name: "Great Prod"
--- Remarks: "Super!"
How do I go about the above to achieve it? Thank you!
Edit 1: Corrected the expected hash for the above and added simple data structure view.
So instead of using hash for year, I actually created a new model for year and from there, store each properties as hash.