Using ActiveAdmin's has_many method in View hangs #804

Closed
akashkamboj opened this Issue Feb 18, 2012 · 1 comment

Projects

None yet

2 participants

@akashkamboj

This has_many method activeadmin is using which is in ...
class FormBuilder < ::Formtastic::FormBuilder

it's pretty awsome and solves the nested models in the Ryan Bates told in his cast No. 196 and 197. But I can use this method only in ActiveAdmin class but not in a HAML view. It would be so great if it can be a part of formtastic instead or any way to achieve it in a HAML view. Any Suggestions? Below is the code ActiveAdmin is using for this method.

    def has_many(association, options = {}, &block)
      options = { :for => association }.merge(options)
      options[:class] ||= ""
      options[:class] << "inputs has_many_fields"

      # Add Delete Links
      form_block = proc do |has_many_form|
        block.call(has_many_form) + if has_many_form.object.new_record?
                                      template.content_tag :li do
                                        template.link_to I18n.t('active_admin.has_many_delete'), "#", :onclick => "$(this).closest('.has_many_fields').remove(); return false;", :class => "button"
                                      end
                                    else
                                    end
      end

      content = with_new_form_buffer do
        template.content_tag :div, :class => "has_many #{association}" do
          form_buffers.last << template.content_tag(:h3, association.to_s.titlecase)
          inputs options, &form_block

          # Capture the ADD JS
          js = with_new_form_buffer do
            inputs_for_nested_attributes  :for => [association, object.class.reflect_on_association(association).klass.new],
                                          :class => "inputs has_many_fields",
                                          :for_options => {
                                            :child_index => "NEW_RECORD"
                                          }, &form_block
          end

          js = template.escape_javascript(js)
          js = template.link_to I18n.t('active_admin.has_many_new', :model => association.to_s.singularize.titlecase), "#", :onclick => "$(this).before('#{js}'.replace(/NEW_RECORD/g, new Date().getTime())); return false;", :class => "button"

          form_buffers.last << js.html_safe
        end
      end
      form_buffers.last << content.html_safe
    end
@justinfrench
Owner

Hi, this is a lot o code to grok. Since your main issue seems to be with ActiveAdmin (a library I'm not familiar with), could you please start with them and perhaps work on a smaller code example and test case that exposes your problem? Closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment