【问题标题】:Limit dynamic field with cocoon gem in rails 3在rails 3中用茧宝石限制动态场
【发布时间】:2012-07-30 02:08:13
【问题描述】:

我正在使用 cocoon gem 在我的网络表单中添加动态字段,现在我想限制它们只添加六个字段,我该怎么做?

下面是我在助手中使用的代码:

   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

结束

提前感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails-3 cocoon-gem


    【解决方案1】:

    您可以在插入前后使用 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
           ... */
    
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      相关资源
      最近更新 更多