Last active
June 10, 2018 14:17
-
-
Save adamgavlak/627f885b94dfbe3bbc9eef4da9a2b5fa to your computer and use it in GitHub Desktop.
A way to extend default FormBuilder when using `form_with`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# custom_form_builder.rb | |
class CustomFormBuilder < ActionView::Helpers::FormBuilder | |
def error_tag(field, opts = {}) | |
@template.content_tag(:span, @object.errors[field].join(", "), class: "text-danger") if [email protected][field].blank? | |
end | |
end | |
# _form.html.erb | |
<%= form_with(model: <model>, local: true, builder: CustomFormBuilder) do |form| %> | |
<div class="field"> | |
<%= form.label :name %> | |
<%= form.text_field :name %> | |
<%= form.error_tag :name %> | |
</div> | |
... | |
# or you can set it as default in config/application.rb | |
config.action_view.default_form_builder = "CustomFormBuilder" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment