【问题标题】:How to get Paper_trail to work with Action Text?如何让 Paper_trail 与动作文本一起使用?
【发布时间】:2019-04-06 00:56:00
【问题描述】:

我用我的基本模型文章很好地设置了Paper Trail Gem,该文章有一个名为bodytext 列。但是,在我在我的应用程序中实现 Action Text 并从 Article 模型中删除列 body 之后,我无法让 Paper Trail 跟踪关联的 body 列中的更改。我让它工作?

免责声明:我是 Rails 的新手。

文章.rb

...
  has_rich_text :body
  has_paper_trail
...

文章架构(删除 :body 列后)

  create_table "articles", force: :cascade do |t|
    t.string "title"
    t.string "slug"
    t.datetime "archived_at"
    t.datetime "published_at"
    ...
  end

动作文本架构

create_table "action_text_rich_texts", force: :cascade do |t|
    t.string "name", null: false
    t.text "body"
    t.string "record_type", null: false
    t.bigint "record_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["record_type", "record_id", "name"], name: "index_action_text_rich_texts_uniqueness", unique: true
  end

我很想将与以前相同的功能返回到应用程序中,在该应用程序中我能够看到文章正文中所做的更改。例如。有人添加了一个句子,删除了一个词,等等。

【问题讨论】:

  • ActionTextRichText 型号可用吗?如果是这样,也许你可以添加has_paper_trail 到它
  • @mr_sudaca 型号不可用。

标签: ruby-on-rails ruby paper-trail-gem trix


【解决方案1】:

结合对我有用的答案:

# frozen_string_literal: true

ActiveSupport.on_load(:action_text_rich_text) do
  ActionText::RichText.class_eval do
    has_paper_trail
  end
end

lib/rich_text_paper_trail.rb 中并确保已加载此文件!例如通过明确要求:require 'rich_text_paper_trail'

【讨论】:

  • 哪里需要这个?
  • 我在安装了 action_text has_rich_text 的模型中需要它,但我认为在 application_controller.rb 或更全局的东西中也可能需要它。也许您也可以将此代码放在 config/initializers/ 的初始化程序文件中
【解决方案2】:

尝试添加config/initializers/rich_text_paper_trail.rb 文件:

ActiveSupport.on_load(:action_text_rich_text) do
  has_paper_trail
end

【讨论】:

  • 没有用。新版本(更新或创建)未保存到 versions 表中。
【解决方案3】:

在尝试了此处提供的所有选项后,我对这种解决方法有了一个想法,最终效果非常好。

我所做的是我只是用纯Trix Editor 版本替换了Action Text,因此我能够将:body 保留在我的article.rb 模型中,保存整个对象的版本并显示差异。耶! ?

【讨论】:

  • 您是如何解决 ActiveStorage 附件部分的?
【解决方案4】:

Ruby 允许您(无论好坏)即时修改事物。

把它放在初始化器中:

ActionText::RichText.class_eval do 
  has_paper_trail
end 

【讨论】:

  • 我试过了,但现在我什至无法运行rails server,因为我会得到:...gems/activerecord-5.2.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `has_many_attached' for #<Class:0x00007fa19c03afd0> (NoMethodError)
  • 尝试在app/models/action_text/rich_text.rb中创建并删除它
  • 我会得到:Circular dependency detected while autoloading constant ActionText::RichText
  • 您可以尝试将其放入初始化程序中并将其包装在 ActiveSupport.on_load(:active_storage_blob) do ... end
【解决方案5】:

我们在 Rails 5.2 上使用活动文本的存档版本做了类似的事情。

宝石文件

gem 'actiontext', git: 'https://github.com/kobaltz/actiontext.git', branch: 'archive', require: 'action_text'

models/article.rb

class Article < ApplicationRecord
  has_paper_tail
  serialize :body, ActionText::Content
  ...
end

helpers/trix_editor_helper.rb

require 'action_view/helpers/tags/placeholderable'

module TrixEditorHelper
  mattr_accessor(:id, instance_accessor: false) { 0 }

# Returns a +trix-editor+ tag that instantiates the Trix JavaScript editor as well as a hidden field
# that Trix will write to on changes, so the content will be sent on form submissions.
#
# ==== Options
# * <tt>:class</tt> - Defaults to "trix-content" which ensures default styling is applied.
#
# ==== Example
#
#   rich_text_area_tag "content", message.content
#   # <input type="hidden" name="content" id="trix_input_post_1">
#   # <trix-editor id="content" input="trix_input_post_1" class="trix-content" ...></trix-editor>

  def trix_editor_tag(name, value = nil, options = {})
    options = options.symbolize_keys

    options[:input] ||= "trix_input_#{TrixEditorHelper.id += 1}"
    options[:class] ||= "trix-content"

    options[:data] ||= {}
    options[:data][:direct_upload_url] = main_app.rails_direct_uploads_url
    options[:data][:blob_url_template] = main_app.rails_service_blob_url(":signed_id", ":filename")

    editor_tag = content_tag("trix-editor", "", options)
    input_tag = hidden_field_tag(name, value, id: options[:input])

    input_tag + editor_tag
  end
end

module ActionView::Helpers
  class Tags::TrixEditor < Tags::Base
    include Tags::Placeholderable
    delegate :dom_id, to: ActionView::RecordIdentifier

    def render
      options = @options.stringify_keys
      add_default_name_and_id(options)
      options['input'] ||= dom_id(object, [options['id'], :trix_input].compact.join('_')) if object
      @template_object.trix_editor_tag(options.delete("name"), editable_value, options)
    end

    def editable_value
      value&.try(:to_trix_html)
    end
  end

  module FormHelper
    def trix_editor(object_name, method, options = {})
      Tags::TrixEditor.new(object_name, method, self, options).render
    end
  end

  class FormBuilder
    def trix_editor(method, options = {})
      @template.trix_editor(@object_name, method, objectify_options(options))
    end
  end
end

然后我们从 cdn 安装了 trix 版本 1.1 并使用标准的 trix-attachments.js 和直接上传。

希望升级到 Rails 6 并尽可能保留这个概念。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-11
    • 2020-11-09
    • 1970-01-01
    • 2017-04-03
    • 2023-02-02
    • 2012-06-03
    • 2020-05-24
    • 2013-07-15
    相关资源
    最近更新 更多