A good example/starter pack for a custom form builder for use with Rails form_for
.
Aside from usage in normal Rails apps, custom form builders are also useful in creating Rails engines where customizing simple_form
or another form gem dependency is not possible.
Currently the html classes/semantics are setup for Bootstrap 5, however it is designed to be generic and easy to modify to work with any CSS framework.
- Copy the
CustomFormBuilder
class toapp/helpers/
or wherever else you like - Copy the
custom_form_for
helper method toapp/helpers/application_helper.rb
- Copy the views to
app/views/form_builder/
- (Optional) Copy the
Forms::Base
class toapp/lib/forms/
as it provides a very useful base class for forms that arent backed by an activerecord model. Provides required features for validations and someform_for
requirements
<%= custom_form_for(@form, as: :my_form, url: request.path, defaults: {view_mode: false, input_html: {}}) do |f| %>
<%= f.field :my_text, type: :text %>
<%= f.field :my_num, type: :number %>
<%= f.field :my_date, type: :date %>
<%= f.field :my_datetime_local, type: "datetime-local" %>
<%= f.field :my_time, type: :time %>
<%= f.field :my_textarea, type: :textarea %>
<%= f.field :my_checkbox, type: :checkbox %>
<%= f.field :my_select, type: :select, collection: [], selected: [], disabled: [], prompt: "Select...", include_blank: false, input_html: {multiple: true} %>
<%= f.view_field :my_readonly_field, label: "Foo", value: "bar" %>
<%=
### Showing all field options
f.field(
:my_text,
type: :text,
value: "foo",
name: "bar",
field_wrapper_html: {},
label_wrapper_html: {},
label_html: {},
input_wrapper_html: {},
input_html: {},
required: true,
required_text: "*",
field_layout: :horizontal,
#field_layout: :vertical,
)
%>
<% end %>
- https://www.brandnewbox.com/notes/2021/03/form-builders-in-ruby/
- https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html
Created & Maintained by Weston Ganger - @westonganger