although the question is quite old, someone might find it useful, as well as im quite new in ruby โโand rails, so there might be a better way to do this.
In the model:
#user.rb attr_accessible :preferences store :preferences
then in the form of a partial:
#views/users/_form.rb <% @user.preferences.each do |k, v| %> <% form.fields_for :preferences, @user.preferences[k] do |p| %> <div class="field"> <%= p.label k %> <br/> <%= p.text_field k, :value => v %> </div> <% end %> <% end %>
Now add additional fields from the ive form created in attr_accessor model 2:
attr_accessible ... , :new_pref_key, :new_pref_val attr_accessor ... , :new_pref_key, :new_pref_val
then added 2 new fields in the form
<%= f.label :new_pref_key %> <%= f.text_field :new_pref_key %> <%= f.label :new_pref_val %> <%= f.text_field :new_pref_val %>
on my controller, I created a function that checks for new fields and then combines the previous prefs with the new ones, for example:
did that i return it and pass it to update_attributes, hope this helps!
user980085
source share