2012-07-30 75 views
0

我正在使用茧宝石在我的webform中添加动态字段,现在我想限制它们只添加六个字段,我该怎么做?用轨道中的茧宝石限制动态字段3

下面

是我用我的助手代码:

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) 
else 
    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) 
end 

感谢您的帮助提前。

回答

0

你可以在插入之前和之后使用JavaScript和Cocoon帮助器方法。所以你可以做什么

.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 
     ... */ 

})