justinfrench/formtastic — Issue #804: Using ActiveAdmin's has_many method in View hangs 
Author: akashkamboj
Created: 2012-02-18 19:27:00+00:00
State: open

This has_many method activeadmin is using which is in ...
```ruby
class FormBuilder < ::Formtastic::FormBuilder
```
it's pretty awsome and solves the nested models in the Ryan Bates told in his cast No. [196](http://railscasts.com/episodes/196-nested-model-form-part-1) and [197] http://railscasts.com/episodes/197-nested-model-form-part-2. 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.

```ruby
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
```