Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templates break when nesting inside a regular Rails fields_for #63

Open
jensljungblad opened this issue May 3, 2016 · 7 comments · May be fixed by #71
Open

Templates break when nesting inside a regular Rails fields_for #63

jensljungblad opened this issue May 3, 2016 · 7 comments · May be fixed by #71

Comments

@jensljungblad
Copy link
Contributor

jensljungblad commented May 3, 2016

I'm having some trouble with deeply nested form fields. It works fine when nesting only using nested_fields_for but if I add a regular fields_for in between, the generated <script> template tag breaks.

form_for @resource do |f|
  f.nested_fields_for :foo do |ff|
    ff.fields_for :bar do |fff|
      fff.nested_fields_for :baz do |ffff|
        # stuff
      end
    end
  end
end

In this case, bar is a belongs_to relation on foo, which means I cannot use nested_fields_for which expects a relation.

@ncri
Copy link
Owner

ncri commented May 3, 2016

Looks like you used the wrong builders in the nested forms. I think it should be:

form_for @resource do |f|
  f.nested_fields_for :foo do |ff|
    ff.fields_for :bar do |fff|
      fff.nested_fields_for :baz do |ffff|
        # stuff
      end
    end
  end
end

@jensljungblad
Copy link
Contributor Author

Oops, that was a typo from my end. Now the question should be corrected. The problem persists. We're looking into if the for_template option gets lost along the way somehow.

@ncri
Copy link
Owner

ncri commented May 3, 2016

I haven't tested this case yet. I can look into it as soon as I have some time. Let me know if you find the issue. ;-)

@jensljungblad
Copy link
Contributor Author

jensljungblad commented May 3, 2016

It works with the following changes:

form_for @resource do |f|
  f.nested_fields_for :foo do |ff|
    ff.fields_for :bar do |fff|
      fff.nested_fields_for :baz, for_template: true do |ffff|
        # stuff
      end
    end
  end
end

That is, we pass along for_template

def nested_model_template name, association_name, options, block
  for_template = options[:for_template] || self.options[:for_template]

Not sure if this is a workable way to solve it?

We also couldn't find where self.options[:for_template] is actually saved. Is it a Rails thing?

@jensljungblad
Copy link
Contributor Author

Actually, if you set for_template on fields_for instead, it will pass it along and things seem to work. So no change needed?

form_for @resource do |f|
  f.nested_fields_for :foo do |ff|
    ff.fields_for :bar, for_template: true do |fff|
      fff.nested_fields_for :baz do |ffff|
        # stuff
      end
    end
  end
end

@ncri
Copy link
Owner

ncri commented May 3, 2016

Okay, good, but it is a hack :) for_template is an internal option only. Hm. I'd like to find a better solution. But might be tricky.

@ncri
Copy link
Owner

ncri commented May 5, 2016

Probably to do this cleanly we will have to patch fields_for to pass down the for_template option from the parent builder... But glad you found a workaround. Although, more correct would probably be:

form_for @resource do |f|
  f.nested_fields_for :foo do |ff|
    ff.fields_for :bar, for_template: ff.options[:for_template] do |fff|
      fff.nested_fields_for :baz do |ffff|
        # stuff
      end
    end
  end
end

Because for_template should only be true when the form is rendered as a nested form fields template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants