Last active
July 14, 2023 10:00
-
-
Save gregblass/b43ac32c85d666139f1b13ee081d8513 to your computer and use it in GitHub Desktop.
Trix Editor custom form input component for SimpleForm
This file contains hidden or 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
# I was using https://github.com/maclover7/trix to do: | |
# | |
# f.input :my_input, as: :trix_editor | |
# | |
# Its currently been over two weeks since Rails 5.2 was released, and the | |
# gem was the only thing preventing me from using it in multiple projects: | |
# https://github.com/maclover7/trix/pull/61#issuecomment-384312659 | |
# | |
# So I made this custom simpleform input for my apps to prevent this from happening again in the future. | |
# | |
# For more info on SimpleForm custom form inputs, see: | |
# https://github.com/plataformatec/simple_form/wiki/Adding-custom-input-components | |
# | |
class TrixEditorInput < SimpleForm::Inputs::Base | |
def input(wrapper_options) | |
template.concat @builder.text_field(attribute_name, input_html_options) | |
template.content_tag(:"trix-editor", nil, input: id) | |
end | |
def input_html_options | |
super.merge({ | |
type: :hidden, | |
id: id, | |
name: name | |
}) | |
end | |
def id | |
"#{@builder.object.class.to_s.downcase}_#{attribute_name}" | |
end | |
def name | |
"#{@builder.object.class.to_s.downcase}[#{attribute_name}]" | |
end | |
end |
Are you still using that gem?
Are you still using that gem?
Thank you, it worked perfectly.
How could we make this work with simple_fields_for?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use
object_name
instead of@builder.object.class.to_s.downcase