【问题标题】:Displaying existing content in Rails 5.2 with ActionText使用 ActionText 在 Rails 5.2 中显示现有内容
【发布时间】:2019-11-10 23:25:28
【问题描述】:

我已将我的应用程序升级到 rails 5.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 中的新记录中获取,但不会从旧帖子的现有“描述”列中显示

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 actiontext


    【解决方案1】:

    我遇到了类似的问题,但在 rails repo 或任何地方都找不到干净的解决方案。作为一种解决方法,在您的情况下,我会尝试:

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

    这不会解决问题,但它会帮助您填充旧帖子值。

    【讨论】:

      【解决方案2】:

      这个答案对我有用。它还具有清理用于普通(非富)文本内容的表的数据库的额外好处。

      “假设你的模型中有一个‘内容’,这就是你想要迁移的内容,首先,添加到你的模型中:”

      has_rich_text :content
      

      "然后创建迁移"

      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
      

      您可以在此处找到我使用的原始解决方案 https://github.com/rails/rails/issues/35002#issuecomment-562311492

      【讨论】:

        猜你喜欢
        • 2020-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多