Skip to content

Instantly share code, notes, and snippets.

@gregblass
Last active July 14, 2023 10:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gregblass/b43ac32c85d666139f1b13ee081d8513 to your computer and use it in GitHub Desktop.
Save gregblass/b43ac32c85d666139f1b13ee081d8513 to your computer and use it in GitHub Desktop.
Trix Editor custom form input component for SimpleForm
# 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
@spdawson
Copy link

You can use object_name instead of @builder.object.class.to_s.downcase

@giedriusr
Copy link

Are you still using that gem?

@frankyston
Copy link

Are you still using that gem?

https://github.com/maclover7/trix

@frankyston
Copy link

Thank you, it worked perfectly.

@svpersteve
Copy link

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