Search code examples
ruby-on-railssimple-formcocoon-gem

Rails - Cocoon - Simple Form - does not render


I'm using Cocoon and SimpleForm for new Orders, which will have multiple OrderItems.

I've installed the gem like so :

gem "cocoon"

And also add to application.js :

//= require cocoon

Models are configured like so :

class Order < ActiveRecord::Base

#Associations
has_many :order_items
accepts_nested_attributes_for :order_items, reject_if: :all_blank, allow_destroy: true
end


class OrderItem < ActiveRecord::Base

#Associations
belongs_to :order
belongs_to :item
end

_form.html.slim for Orders is :

= simple_form_for(@order) do |f|
   = f.error_notification
   .row
    .col-md-6
        .form-inputs
            = f.association :branch
            = f.association :client

    .col-md-6
        = f.simple_fields_for :order_items do |order_item|
            = render 'order_item_fields', f: order_item
            .links
                = link_to_add_association 'add order_item', f, :order_items

.form-actions
= f.button :submit

and the partial _order_items_fields.html.slim is :

.nested_fields
  = f.input :item_id
  = f.input :dicount_percentage
  = f.input :fulfilment_type
  = f.input :promised_delivery_date
  = f.input :actual_delivery_date
  = f.input :notes
  = link_to_remove_association "remove order item", f

When I run orders/new, all the fields are displayed except the ones that should be displayed by cocoon.

I've done everything by the instructions on the github page.

What could the problem be?

I've also checked and the cocoon JS file is being loaded.


Solution

  • I don't know much about HAML, but I believe that you need to line up the .links with = f.simple_fields_for :order_items do |order_item| since HAML is whitespace dependent.

    Using this simple form example linked to from the Cocoon github