mirror of
https://github.com/jbranchaud/til
synced 2026-01-08 09:38:04 +00:00
Add Provide Fake Form Helper To Controllers as a Rails TIL
This commit is contained in:
40
rails/provide-fake-form-helper-to-controllers.md
Normal file
40
rails/provide-fake-form-helper-to-controllers.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# Provide Fake Form Helper To Controllers
|
||||
|
||||
I'm rendering a partial from a turbo stream. The partial is meant to be
|
||||
rendered within a Rails form object because it contains an input element that
|
||||
needs to reference the form object. The problem is that from the controller
|
||||
that is streaming the partial, there is no
|
||||
[FormBuilder](https://api.rubyonrails.org/v6.1.0/classes/ActionView/Helpers/FormBuilder.html)
|
||||
object.
|
||||
|
||||
One way to get around this that I've borrowed from [Justin
|
||||
Searls](https://justin.searls.co/posts/instantiate-a-custom-rails-formbuilder-without-using-form_with/)
|
||||
is with a `FauxFormHelper`.
|
||||
|
||||
```ruby
|
||||
module FauxFormHelper
|
||||
FauxFormObject = Struct.new do
|
||||
def errors
|
||||
end
|
||||
|
||||
def method_missing(...)
|
||||
end
|
||||
|
||||
def respond_to_missing?(...)
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def faux_form
|
||||
@faux_form ||= ActionView::Helpers::FormBuilder.new(
|
||||
nil,
|
||||
FauxFormObject.new,
|
||||
self,
|
||||
{}
|
||||
)
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
This module defines and exposes a `faux_form` object that controllers and views
|
||||
can access. Then my partial can recieve that form object as a parameter.
|
||||
Reference in New Issue
Block a user