Displaying existing content in Rails 5.2 with ActionText

Viewed 564

I have upgraded my app to rails 5.2 and inclined to use ActionText since old trix-editor/gem is no longer working. Now new posts display their "descriptions" but how can I display my old posts' DESCRIPTIONS with the new installed ActionText?

post.rb has_rich_text :description

posts_controller.rb ...params.require(:post).permit(:description)

_form.html.erb <%= f.rich_text_area :description %>

show.html.erb <%= @post.description %>

Descriptions are only fetching from new records in ActionText but not displaying from existing "description" columns for old posts

2 Answers

I had a similar issue and I couldn't find a clean solution in the rails repo or anywhere. As a workaround, in your case, I would try:

show.html.erb: .
<%= @post.try(:description).body || @post[:description] %>

That wont solve the issue, but it would help you to populate old post values.

This answer worked for me. It also has the added bonus of cleaning up your database of the tables used for the normal (non-rich) text content.

"Assuming there's a 'content' in your model and that's what you want to migrate, first, add to your model: "

has_rich_text :content

"then create a migration"

rails g migration MigratePostContentToActionText
class MigratePostContentToActionText < ActiveRecord::Migration[6.0]
  include ActionView::Helpers::TextHelper
  def change
    rename_column :posts, :content, :content_old
    Post.all.each do |post|
      post.update_attribute(:content, simple_format(post.content_old))
    end
    remove_column :posts, :content_old
  end
end

You can find the original solution I used here https://github.com/rails/rails/issues/35002#issuecomment-562311492

Related