Module: Trestle::FormHelper

Defined in:
app/helpers/trestle/form_helper.rb

Instance Method Summary collapse

Instance Method Details

#formObject

Returns the currently scoped Trestle form builder (a subclass of ActionView::Helpers::FormBuilder).



39
40
41
# File 'app/helpers/trestle/form_helper.rb', line 39

def form
  @_trestle_form
end

#trestle_form_for(instance, **options, &block) ⇒ Object

Generates a form for a resource using Rails’ #form_for helper.

In addition to delegating to #form_for, this helper method:

1) Sets the default form builder to ‘Trestle::Form::Builder`. 2) Sets the default :as option to match the parameter name

expected by the admin.

3) Sets default Stimulus controllers on the <form> element

from `Trestle.config.default_form_controllers`.
(defaults to: "keyboard-submit form-loading form-error")

4) Sets a null/identity ActionView::Base.field_error_proc as

errors are handled by Trestle::Form::Fields::FormGroup.

5) Exposes the yielded form builder instance via the ‘form` helper.

Examples

<%= trestle_form_for(article, url: admin.instance_path(instance, action: :update),
                              method: :patch) do %> ...

Returns a HTML-safe String. Yields the form builder instance.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/trestle/form_helper.rb', line 23

def trestle_form_for(instance, **options, &block)
  options[:builder] ||= Form::Builder
  options[:as] ||= admin.parameter_name

  options[:data] ||= {}
  options[:data][:controller] = (Trestle.config.default_form_controllers + Array(options[:data][:controller])).join(" ")

  form_for(instance, **options) do |f|
    with_identity_field_error_proc do
      with_form(f) { yield f }
    end
  end
end