Comments (21)
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.
- Move the
NestedForm::Builder
behavior into a module andextend
it into each form builder object dynamically insidenested_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 insidefields_for
as well. - Only use helper methods. This means
f.link_to_remove "remove"
would change tolink_to_remove f, "remove"
etc. This would also require a customnested_fields_for
helper.
I'm leaning toward the fist approach if I can get that to work. Anyone have ideas?
from nested_form.
Couldn't you just add a :custom_builder option to nested_form_for?
from nested_form.
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.
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.
@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.
@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.
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.
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.
@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.
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.
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.
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.
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.
Yeah I was thinking something along those lines. Time to do some experimenting.
from nested_form.
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.
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.
Any news on this ? Any temporary solution to bypass the issue ?
from nested_form.
Any update here?
from nested_form.
+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.
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.
@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)
- Can I add different subform (using STI)? HOT 3
- Remove labels
- Cusomized forms
- link_to_add make large index number
- wrong field name for the nested _destroy field
- Incremental counter for nested fields HOT 2
- nested_form shows extra fields after error messages HOT 1
- -
- Limit how many nested_attributes can be added? HOT 1
- undefined method `capture_position=' for nil:NilClass HOT 1
- link_to_remove is not deleted vale only hide that so when we update that send hide value in parmeter HOT 1
- nested_form for recursive Model HOT 1
- on clicking link_to_add nested forms not shown in rails HOT 1
- Sort nested form in edit view based on column HOT 1
- Bootsy wysiwyg editor not supported for nested forms HOT 3
- reject_if if 2 attributes are blank HOT 2
- Callbacks nested
- Link to remove not working as expected when :nested_wrapper is false HOT 1
- When I cleck on <%= f.link_to_add "Add a Step", :steps %>, nothing happens.
- Link_to_add, data: { target: } not working for a bootstrap modal pop up HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nested_form.