Take the 2-minute tour ×

I am using cocoon gem for adding dynamic fields in my webform, now i want to restrict them to add only six fields, how can I do it?

below is the code I am using in my helper:

   def link_to_add_association(*args, &block)
if block_given?
  f            = args[0]
  association  = args[1]
  html_options = args[2] || {}
  link_to_add_association(capture(&block), f, association, html_options)
  name         = args[0]
  f            = args[1]
  association  = args[2]
  html_options = args[3] || {}

  html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
  html_options[:'data-association'] = association.to_s.singularize
  html_options[:'data-associations'] = association.to_s.pluralize

  new_object = f.object.class.reflect_on_association(association).klass.new
  html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object)).html_safe

  link_to(name, '#', html_options )


Thanks for your help in advance.

share|improve this question

1 Answer 1

You could do that using JavaScript and Cocoon helper method before and after insert. So what you could do

.bind('cocoon:after-insert', function() {
    /* ...
       1. check to see if the number of nested fields added is equal to what you want
       2. if it is equal, hide the link_to_add_association link
       ... */

share|improve this answer

Your Answer


