Simple_form: rich_text_area input field type for ActionText shipping with Rails 6.x

Created on 30 Jan 2019  路  17Comments  路  Source: heartcombo/simple_form

Environment

  • Ruby [version]

ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]

  • Rails [version]

6.x

  • Simple Form [version]

simple_form (4.1.0)

Current behavior

Rails 6 ~is close to shipping~ has released. ActionText is one of the most exciting features of Rails 6, and it is implemented to Rails forms as follows:

<%= form_with(model: message) do |form| %>
  <%= form.label :content %>
  <%= form.rich_text_area :content %>
<% end %>

However, simple_form doesn't seem ready to accept rich_text_area as an argument to as:.

Expected behavior

simple_form should support rich_text_area as an input type and render the ActionText form.

Feature request

Most helpful comment

In the meantime, this is pretty trivial to implement yourself:

class RichTextAreaInput < SimpleForm::Inputs::Base
  def input(wrapper_options = nil)
    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

    @builder.rich_text_area(attribute_name, merged_input_options)
  end
end

All 17 comments

I'm not sure if we should include this feature at Simple Form now. @rafaelfranca any thoughts?

Hi, lads! Any news about this issue? Rails is rc1

Is simple_form going to support rich_text_area for Rails 5.2?

So Rails 6 is out in the wild, but no news here.

In the meantime, this is pretty trivial to implement yourself:

class RichTextAreaInput < SimpleForm::Inputs::Base
  def input(wrapper_options = nil)
    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

    @builder.rich_text_area(attribute_name, merged_input_options)
  end
end

Trivial for you, Jared...
...but I don't know where I would put that code. In an initializer, such as simple_form.rb? I don't understand the reluctance to update the gem to be used with action_text, especially for blokes like me who only know only enough RoR to operate a single website as a volunteer.

@ThomasConnolly where to put the code is more a Rails topic than SimpleForm, but I have my custom inputs in app/inputs; since app is in the Rails default autoload path, there's nothing more to do.

You could also put custom inputs under lib/inputs perhaps, in which case you would need to add a require statement to the file(s) - or add lib/inputs to config.autoload_paths in config/application.rb so that rails will autoload everything in that folder.

See the Rails Guide on autoloading for more information.

_[Edit: to clarify, I'm not a contributor to this gem and can't speak to this being included - it would make sense to me to include it, I just posted my solution here in case it could help]_

@jaredmoody your input implementation loses the default trix-content class that the Rails form builder adds on the trix-editor element because you're passing in the simple_form classes instead; it messes up some of the styling of content in the editor. In my case, I just added it as a default input_html_class to the input. Of course, this doesn't allow you to remove the class, so if you need to be able to use that class in some places and not others, you'll want a different solution.

class RichTextAreaInput < SimpleForm::Inputs::Base
  def input_html_classes
    super.push('trix-content')
  end

  def input(wrapper_options = nil)
    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

    @builder.rich_text_area(attribute_name, merged_input_options)
  end
end

https://github.com/heartcombo/simple_form/pull/1646 was just merged. Might consider remedied?

I'd love to see this, now July 2020

@evanjroberts This was merged in Feb - so it's in there!

@evanjroberts what @jaredmoody said :), make sure you update to 5.0.2 to get it, and let us know if you bump into issues.

thanks @carlosantoniodasilva this might be the wrong place as you all seem so professional but should I run it as
<%= f.input :body, as: :rich_text_area %> OR <%= f.rich_text_area :body %>

thanks! (I'm running 5.0.2 now thanks for the info)

For simple programmers like me it may be useful to add a simple 'howto' to the Readme file on the gem page

Hi @evanjroberts

<%= f.input :body, as: :rich_text_area %> should work. Make sure you are using the last version released (5.0.2) and if you still cannot make it, feel free to send an example application then we can help you 馃檶 .

Regarding docs, you are right. We should document this somewhere. I think it would be nice if we add a section about rich_text_area under collections. Would you willing to do that?

Maybe we can just add the mapping to the table here: https://github.com/heartcombo/simple_form#available-input-types-and-defaults-for-each-column-type, since there's plenty of examples about how to use the different mappings?

Hello everyone! I am trying to implement the new rich_text_area, but can't seem to make it work at all. I tried both
<%= f.input :body, as: :rich_text_area %> and
<%= f.rich_text_area :body %>
But still not getting a rich text area. The field simply disappears completely.

I am running Rails 6 and have installed the simple_form gem as is in the gemfile (gem 'simple_form')

Could someone help me out?

Hi @IlianaP 馃憢

Make sure you have installed all dependencies required for rich text area: https://edgeguides.rubyonrails.org/action_text_overview.html#trix-compared-to-other-rich-text-editors

There is also this tutorial that might help you: https://gorails.com/episodes/how-to-use-action-text

Was this page helpful?
0 / 5 - 0 ratings