ruby on rails - Has_one Association breaking in Edit -
i have nested form using cocoon gem gests 5 classes deep. belongs_to , has_many except 1 2nd nested class has_one , belongs_to association. whenever edit it, gets deleted , recreates instance (not want) ideas?
class firmwarescontroller < applicationcontroller def index @firmwares = firmware.all end def new @firmware = firmware.new end def edit @firmware = firmware.find(params[:id]) end end class setting < activerecord::base belongs_to :menu_item has_many :selections, dependent: :destroy has_many :dependencies attr_accessible :kind, :name, :placement, :selections_attributes accepts_nested_attributes_for :selections, reject_if: :all_blank, allow_destroy: true validates_presence_of :kind end class menuitem < activerecord::base belongs_to :menu belongs_to :dependency has_one :setting, dependent: :destroy attr_accessible :dependency_id, :menu_for, :name, :placement, :setting_attributes accepts_nested_attributes_for :setting, reject_if: :all_blank, allow_destroy: true validates_presence_of :name end _menu_items.html.haml
.row-fluid .input-prepend = f.input :name, label: "menu item name" .input-append = f.input :placement, label: "order in menu (x.y)", as: :string = f.simple_fields_for :setting |settings_form| = render 'setting_fields', f: settings_form %p = link_to_add_association "has setting?", f, :setting, class: "btn btn-primary btn-small add_setting_link" = link_to_remove_association 'remove menu item', f, class: "btn btn-danger btn-small remove_menu_item" _setting_fields.html.haml
.row-fluid .input-prepend = f.input :kind, label: "type", collection: setting_kinds, input_html: {class: "setting_type"} .input-append = link_to_remove_association 'remove setting', f, class: 'btn btn-danger btn-small remove_setting_link' .setting_selection{style: "display: none;"} = f.simple_fields_for :selections |selections_form| = render 'selection_fields', f: selections_form %p= link_to_add_association 'add selection option', f, :selections, class: "btn btn-primary btn-small"
i assume has_one association you're referring has_one :setting in menuitem. if so, might try adding update_only: true accepts_nested_attributes_for :setting options. from documentation (emphasis mine):
by default :update_only option
false, nested attributes used update existing record if include record's:idvalue. otherwise new record instantiated , used replace existing one. if :update_only optiontrue, nested attributes used update record’s attributes always, regardless of whether:idpresent.
Comments
Post a Comment