has_many form problem after update to 0.5.1 #1813

OlegPasko opened this Issue Dec 9, 2012 · 18 comments

9 participants


After update from 0.5 to 0.5.1, the add button for new nested attributes stops working.

For example, this no longer works. Rollback to 0.5 solved the problem.
In my variant:

f.has_many :product_values do |v|
  v.inputs "Категории" do
    v.input :_destroy, :as => :boolean, :label => "Destroy?" if !v.object.nil?
    v.input :value

Any ideas, why? :)

Active Admin member

@OlegPasko, what problem exactly did you run into?


Oh... sorry @Daxter:
I can't push button "Add new ...", — no reaction for this action, same as no js-console error :(

Active Admin member

Did you confirm that the "add new" button works when you don't have a form using f.has_many?

By 'no reaction', do you mean that when you push the button you just stay on the same page?

If you're running Chrome, push F12 and navigate to the network tab, then push the button. Does a GET request for /new successfully complete?

It also couldn't hurt to confirm that there is a route available. So when running rake routes confirm that you see a similar line as the one below for the resource you're having trouble with:

new_user GET        /users/new(.:format)                       users#new

Yep, with 0.5 version works like: I push Add button => appear fields block (without page reload).
Changing gem to 0.5.1 —not works... rollback — all is ok :)

With routes all is ok, any messages in network (checked before).

Maybe something with jQuery?

Add button in 0.5 (works) has:

onclick="$(this).before('<fieldset class=\"inputs has_many_fields\"><ol><li class=\"input\"><fieldset class=\"inputs\">
My options ...
<\/fieldset><\/li><li><a href=\"#\" class=\"button\" onclick=\"$(this).closest(&#x27;.has_many_fields&#x27;).remove(); return false;\">Удалить<\/a><\/li><\/ol><\/fieldset>'.replace(/NEW_RECORD/g, new Date().getTime())); return false;

In 0.5.1 (not works):

onclick="$(this).siblings('li.input').append('<fieldset class=\"inputs has_many_fields\"><ol><li class=\"input\"><fieldset class=\"inputs\">
My options ...
<\/fieldset>'.replace(/NEW_PRODUCT_VALUE_RECORD/g, new Date().getTime())); return false;

I have the same problem.

Active Admin member

I can confirm this problem. It was caused by a63cd90 / PR #1771.

@pcreux, I don't understand what the purpose of #1771 was. @davidmh added the "li.input" stipulation, but the DOM doesn't have any li elements at that point. Instead it's

div # contains all child data
  fieldset # one for each child object
      # then li elements here containing labels and inputs

Until we have this fixed in the next version, @OlegPasko and @arwed, I would suggest you stay on 0.5.0.


Having the same problem, $(this).siblings('li.input') has no reference in the DOM for the Add New button. Staying with 0.5.0 so far.


This onclick js should also initialize new dynamically added datepicker inputs:

Active Admin member

@iabdulin, that certainly should be added, but we should only act upon the HTML that was just added like so:

// what it was originally
$(this).before('#{js}'.replace(/#{placeholder}/g, new Date().getTime())); return false;
// what it would be to initialize any datepickers added
$(this).before('#{js}'.replace(/#{placeholder}/g, new Date().getTime()))
       .prev().find('input.datepicker').datepicker(); return false;

I didn't test the above, so it might not quite work. But the purpose is to be performant by only parsing the new HTML.

It might be better to specify the has_many container, and use :last-child.


@Daxter you are right

Active Admin member

I haven't tested it extensively, but jQuery's live on might be better to have in the main initializer:

$(".datepicker").on 'focus', ->
  $(this).datepicker dateFormat: 'yy-mm-dd'

That way current AND future datepickers are set up correctly.


I made some duty hack to fix it

  $('.has_many > .button').each(function(i, el) {
    var func, onclick = el.onclick.toString().replace("$(this).siblings('li.input').append(", '$(this).before(');
    eval("func = " + onclick);
    $(el).attr('onclick', '').bind('click', func);
Active Admin member

@Paxa, what does that even do?


Changes onClick event of "add button" for nested forms (f.has_many do ... )




#1879 has been merged, should resolve this issue.

@macfanatic macfanatic closed this Mar 7, 2013

Whats a reliable way to use this fix? Will a newer version of AA be released anytime soon to provide the solution?

Active Admin member

@macfanatic it's not #1879 that resolved this, but #1853

@gaganawhad you can use 0.5.1 until 0.5.3 is released (expected in a couple weeks), or you can use the latest code:

gem 'activeadmin', github: 'gregbell/active_admin'
@shuebner shuebner pushed a commit to shuebner/charter that referenced this issue Dec 11, 2013
Sven Hübner activeadmin-Version 0.5.0 benutzen für richtiges Javascript in has_ma…
…ny-Formular. siehe auch activeadmin/activeadmin#1813
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment