Using the DateField helper in phlex-rails #497
|
I'm currently refactoring a project to using Phlex - and it's the next big thing IMHO 😃 I've managed to use helpers until now but this one keeps biting me in the butt 😢 It renders - but leaves out the 'meat' so to speak (the date form field does not get buffered) I'm sure it's on me - but I really could do with a hint |
Replies: 11 comments 16 replies
|
Looks like you're not using the date field helper adapter but are calling |
|
Wow - much appreciate your ‘trigger finger’ 🙏😊I can but realise that the @Form is a ‘fields_for’ association do |form| 😱Med venlig hilsenWaltherDen 25. feb. 2023 kl. 11.42 skrev Joel Drapper ***@***.***>:
Looks like you're not using the date field helper adapter but are calling date_field on whatever the @Form object is. If that's a buffered form builder from the form_with or form_for adapters, it should work. Can you share the code where the form is created?
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
|
|
|
the issue at hand is the |
|
I see what the problem is. The form is being created in ERB and then passed to Phlex. The problem is, when Phlex gets it, it's not been adapted. The easiest solution would be to move your whole form into a Phlex component so that the initial With a Hope that makes sense. |
|
Thx a lot 🙏It males sense - and the form will end up in Phlex, eventually; only I hoped to take it in tiny steps 😌 guess not —Ever in Denmark? Med venlig hilsenWaltherDen 25. feb. 2023 kl. 17.35 skrev Joel Drapper ***@***.***>:
I see what the problem is.
The form is being created in ERB and then passed to Phlex. The problem is, when Phlex gets it, it's not been adapted. The easiest solution would be to move your whole form into a Phlex component so that the initial form_with call hits the Phlex::Rails::Helpers::FormWith adapter and yields a BufferedFormBuilder.
With a BufferedFormBuilder, f.date_field will immediately push the date field tag to the output buffer rather than returning a value. This is much more natural in Phlex, while in ERB, you manually output the return value by using <%= tags.
Hope that makes sense.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
|
|
I have a question pertaining to the output - I have this - actually it's the same on all fields inside the fields_for - and I've removed the non-essential parts This all gets rendered ok - but the order is somewhat strange (which does not result in the output I expected) But - if I don't use the form and calls the helper like |
|
@joeldrapper sorry to elevate this - but I'm dead-locked on this 😢 If there is no answer (or immediate solution to the 'misordering' of HTML-soup when using fields_for) I'll have to revert to using view_components - and hate every minute of it now that I know there is a better way 😭 |
|
Hey @wdiechmann, hopefully this will help. https://cloud.drapper.me/pyMtHdwV |
|
@joeldrapper Wondering if you might have one more trick up that sleeve? 'cause the Keeping it to the Now I have refactored like this: But it's so close now! I can almost smell it 😃 |
|
I think you could probably do this from the component. def field_for(*args, **kwargs) # takes a block (we don't need to specify that because we're yielding)
# save the current value of @form. We'll need it later.
original_form = @form
begin
# delegate the arguments to the fields_for method on the @form
@form.fields_for(*args, **kwargs) do |f|
# temporarily set @form to point to the fields_for form builder
@form = f
# yield the component while it has @from scoped to the fields_for form builder
yield(self)
end
ensure
# set the form back to the original form before scoping it
@form = original_form
end
end |

I think you could probably do this from the component.