Giter Site home page Giter Site logo

Comments (21)

ryanb avatar ryanb commented on July 30, 2024

This is something I would very much like to fix but am unsure the best way. The two approaches I can think of are this.

  1. Move the NestedForm::Builder behavior into a module and extend it into each form builder object dynamically inside nested_form_for. The problem is we don't have easy access to the builder instance unless we do a wrapper block and inject the behavior in there. This would need to be done inside fields_for as well.
  2. Only use helper methods. This means f.link_to_remove "remove" would change to link_to_remove f, "remove" etc. This would also require a custom nested_fields_for helper.

I'm leaning toward the fist approach if I can get that to work. Anyone have ideas?

from nested_form.

beno avatar beno commented on July 30, 2024

Couldn't you just add a :custom_builder option to nested_form_for?

from nested_form.

ryanb avatar ryanb commented on July 30, 2024

How would that work when we already have a form builder class? I can't pass in two builder classes into form_for. It needs to be moved into a module so the behavior can be injected into a custom class/instance as needed.

from nested_form.

jonah-williams avatar jonah-williams commented on July 30, 2024

As is you can specify a custom form builder in nested_form_for:

nested_form_for @model, :html => { :class => 'foo' }, :builder => MyCustomFormBuilder do |f|

I opened this issue because if you have specified a custom form builder as your default

ActionView::Base.default_form_builder = MyCustomFormBuilder

then nested_form_for will override the default builder by adding a :builder to the options hash. This forces us to specify our custom form builder on every use of nested_form_for which seems unnecessarily verbose. I'm hoping to find a solution which allows us to specify the custom form builder we want to use by default once rather than on every call to nested_form_for.

from nested_form.

beno avatar beno commented on July 30, 2024

@ryan I was trying to address the 'access to builder' issue of your option 1. So take the custom builder, extend it with the 'nestable' module and call form_for with that extended builder. But maybe I misunderstood.

from nested_form.

ryanb avatar ryanb commented on July 30, 2024

@jonah, even if one passes a custom form builder with the :builder option it will still need to implement the nested builder behavior. It would be great if it worked with any form builder out of the box. I think solving that problem will solve the default_form_builder issue as well.

@beno, that is a possibility, but I don't want to inject behavior into the passed in class. Maybe I can do Class.new(options[:builder]).include(Builder) so it works off a new class. Or extend the module onto the builder instance at the beginning of the block. I'll need to do some experimenting.

from nested_form.

jonah-williams avatar jonah-williams commented on July 30, 2024

Right, in my case I ended up passing a custom form builder with every call and changing my form builder to extend NestedForm::Builder rather than ActionView::Helpers::FormBuilder. Now my custom form builder has all the extra behavior of NestedForm::Builder which it doesn't override and doesn't always need but at least I can use my additions to FormBuilder within nested_form_for.

I totally agree that approach #1 suggested above seems like the cleanest solution, at least from the perspective of users of nested_form_for. Maybe I'm missing something but why is it hard for nested_form_for to identify the builder used? Either you have a builder passed in as part of your args hash and you can add behavior to it or you can get the default_form_builder extend that and pass it along to form_for.

from nested_form.

beno avatar beno commented on July 30, 2024

I think it would be great if using the nested_form gem resulted in having "nested_xxx_form_for" helpers for all form builders I use. So the default would be "nested_form_for", but you could also have "nested_simple_form_for", "nested_semantic_form_for", etc. What do you think?

from nested_form.

ryanb avatar ryanb commented on July 30, 2024

@jonah, it is not difficult I just don't like to extend the class itself and modify its behavior because if the class is then used outside of nested_form_for it will still have the nesting behavior.

@beno, I'm not sure how to do that besides overriding method_missing which I don't want to do in some place common like the template. However I can manually include helpers for some of the more common form builders (simple form, semantic form, etc.). Thanks for the suggestion.

from nested_form.

beno avatar beno commented on July 30, 2024

Or maybe you could find all the helpers ending in "...form_for" at initialize and define the nested versions then. I'm not familiar enough with the innards of rails to know how easy that would be, and it's a little brittle of course, but it would save you the manual inclusions.

from nested_form.

ryanb avatar ryanb commented on July 30, 2024

That starts getting messy because different plugins add helper methods in different ways which are added at different times. I think it would be better to manually add a helper method for each of the most common form builder plugins and document how one can add their own.

from nested_form.

ryanb avatar ryanb commented on July 30, 2024

One other crazy idea is to add a nested helper method which you can call other helper methods through.

<%= nested.form_for @post do |f| %>
<%= nested.simple_form_for @post do |f| %>

Hmm, I'm thinking too much magic and it takes up the generic nested method name. But a fun idea otherwise.

from nested_form.

jonah-williams avatar jonah-williams commented on July 30, 2024

I understand the concern about modifying the supplied builder object but what about passing a proxy builder along to form_for which delegates ActionView::Helpers::FormBuilder method calls to the provided builder (passed to nested_form_for or from default_form_builder) and adds the required NestedForm::Builder methods if they are missing. Is that what you were suggesting as a wrapper block above? That would leave the user's builder object unmodified and could even allow the user to implement NestedForm::Builder methods if they want to override the default behavior all without changing the nested_form_for interface.

from nested_form.

ryanb avatar ryanb commented on July 30, 2024

Yeah I was thinking something along those lines. Time to do some experimenting.

from nested_form.

aaronchi avatar aaronchi commented on July 30, 2024

I did this myself by using alias_method_chain in the ActionView FormHelper and then passing an argument (like :nested => true) to enable the nested functionality. Works great.

from nested_form.

elmatou avatar elmatou commented on July 30, 2024

For the record, the choice mentionned has already been made by another gem providing similar features :

https://github.com/nathanvda/cocoon/

IMHO the implementation is cleaner, even if the library is not yet powerfull as nested_form.

+1 for support of custom builders (especialy at fields_for level)

from nested_form.

 avatar commented on July 30, 2024

Any news on this ? Any temporary solution to bypass the issue ?

from nested_form.

matthew342 avatar matthew342 commented on July 30, 2024

Any update here?

from nested_form.

ccmcbeck avatar ccmcbeck commented on July 30, 2024

+1 for support of custom builders, but for now I'm using Jonah's idea of extending NestedForm::Builder rather than ActionView::Helpers::FormBuilder and it seems to work well

from nested_form.

SeanRoberts avatar SeanRoberts commented on July 30, 2024

I got this working by including NestedForm::BuilderMixin in my custom builder. I assume this is the solution that Ryan was talking about building a year ago. I think he must have done it but not updated this issue. @ryanb did you mean to close this issue?

from nested_form.

lest avatar lest commented on July 30, 2024

@SeanRoberts You're right about NestedForm::BuilderMixin.

I've added example code snippets to the How To: Custom nested form builders wiki page.

from nested_form.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.