2

5 クラスの深さを生成する cocoon gem を使用してネストされたフォームがあります。has_one と belongs_to 関連付けを持つ 2 番目にネストされたクラスを除いて、それらはすべて belongs_to と has_many です。編集するたびに削除され、インスタンスが再作成されます (必要なものではありません) アイデアはありますか?

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 do |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 do |selections_form|
        = render 'selection_fields', f: selections_form
    %p= link_to_add_association 'Add Selection Option', f, :selections, class: "btn btn-primary btn-small"
4

2 に答える 2