我已经把我的应用升级到了Rails5.2,并且倾向于使用ActionText,因为旧的trix-editor/gem已经不能工作了。现在新的帖子会显示它们的“描述”,但是我如何用新安装的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 %>
描述仅从ActionText中的新记录中提取,而不从旧帖子的现有“描述”列中显示
发布于 2019-08-16 23:17:21
我也遇到过类似的问题,但我在rails存储库或任何地方都找不到干净的解决方案。作为一种变通方法,在您的情况下,我会尝试:
show.html.erb:。
<%= @post.try(:description).body || @post[:description] %>
这不会解决问题,但它将帮助您填充旧的帖子值。
发布于 2020-10-21 13:04:49
这个答案对我很有效。它还有一个额外的好处,就是清理数据库中用于普通(非富)文本内容的表。
“假设你的模型中有一个‘内容’,这就是你想要迁移的内容,首先,添加到你的模型中:”
has_rich_text :content“然后创建迁移”
rails g migration MigratePostContentToActionTextclass 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您可以在这里找到我使用的原始解决方案https://github.com/rails/rails/issues/35002#issuecomment-562311492
https://stackoverflow.com/questions/56816455
复制相似问题