Rails Nested Form(Using Jquery)

430 Views Asked by At

I am having three models Deals,CellPhoneAttribute and Cellphone Equipment. The relation between models are:

  class CellphoneEquipment < ActiveRecord::Base
    belongs_to :cellphone_deal_attribute
  end

  class CellphoneAttribute < ActiveRecord::Base
    has_many :cellphone_equipments, dependent: :destroy
    accepts_nested_attributes_for :cellphone_equipments, :reject_if => :reject_equipment, allow_destroy: true

    def reject_equipment(attributes)
      if attributes[:model].blank?
        if attributes[:id].present?
          attributes.merge!({:_destroy => 1}) && false
        else
          true
        end
      end
    end
   end

class Deal < ActiveRecord::Base
  has_many  :cellphone_deal_attributes, dependent: :destroy
  accepts_nested_attributes_for :cellphone_deal_attributes,:reject_if => :reject_cellphone, allow_destroy: true

  private
    def reject_cellphone(attributes)
      if attributes[:domestic_call_minutes].blank?
        if attributes[:id].present?
          attributes.merge!({:_destroy => 1}) && false
        else
          true
        end
      end
  end
end

I have one form for deal and inside that form I have CellphoneAttribute Form and inside CellphoneAttribute I have CellphoneEquipment Form. All is working well till here. Now, I want CellPhoneEquipment form to open multiple times through Jquery. Please guide me how to do this.

1

There are 1 best solutions below

0
Fakhir Shad On

Try using the cocoon gem. It handles Dynamic nested forms using jQuery. Here is the link to gem https://github.com/nathanvda/cocoon. They have a very good documentation so it will be very easy for you to get started. Thanks