Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何让书面记录与动作文本一起工作?_Ruby On Rails_Ruby_Paper Trail Gem_Trix - Fatal编程技术网

Ruby on rails 如何让书面记录与动作文本一起工作?

Ruby on rails 如何让书面记录与动作文本一起工作?,ruby-on-rails,ruby,paper-trail-gem,trix,Ruby On Rails,Ruby,Paper Trail Gem,Trix,我很好地设置了我的基本模型文章,其中有一个名为body的text专栏。但是,在我实现了我的应用程序并从文章模型中删除了body列之后,我无法获得书面记录来跟踪相关body列中的更改。我如何才能让它起作用 免责声明:我是Rails的新手 第1.rb条 ... has_rich_text :body has_paper_trail ... 项目架构(删除后:正文列) 动作文本模式 create_table "action_text_rich_texts", force: :cascade

我很好地设置了我的基本模型文章,其中有一个名为
body
text
专栏。但是,在我实现了我的应用程序并从文章模型中删除了
body
列之后,我无法获得书面记录来跟踪相关
body
列中的更改。
我如何才能让它起作用

免责声明:我是Rails的新手

第1.rb条

...
  has_rich_text :body
  has_paper_trail
...
项目架构(删除后:正文列)

动作文本模式

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
我很想将与之前相同的功能返回到应用程序,在那里我可以看到文章正文中所做的更改。例如,有人添加了一个句子,删除了一个单词等。

Ruby允许您(无论好坏)动态修改内容

将其放入初始值设定项中:

ActionText::RichText.class_eval do 
  has_paper_trail
end 

尝试添加一个
config/initializers/rich\u text\u paper\u trail.rb
文件,其中包含:

ActiveSupport.on_load(:action_text_rich_text)do
有书面记录吗
结束

在尝试了这里提供的所有选项后,我想到了一个解决方案,最后效果非常好


我所做的是,我只是用纯版本替换了动作文本,因此我能够在我的article.rb模型中保留
:body
,保存整个对象的版本并显示差异。耶 把这些答案结合起来对我很有用:

# frozen_string_literal: true

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

lib/rich\u text\u paper\u trail.rb中,确保已加载此文件!例如,通过明确要求它:
require'rich\u text\u paper\u trail'

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

Gemfile

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
要求“操作\视图/助手/标记/可占位符”
模块TrixEditorHelper
mattr_访问器(:id,实例_访问器:false){0}
#返回一个+trix editor+标记,该标记实例化trix JavaScript编辑器以及一个隐藏字段
#Trix将在更改时写入,因此内容将以表单提交的形式发送。
#
#==选项
#*:类-默认为“trix内容”,确保应用默认样式。
#
#==示例
#
#富文本区域标记“内容”,message.content
#   # 
#   # 
def trix_editor_标记(名称,值=nil,选项={})
选项=选项。符号化\u键
选项[:input]| |=“trix_input_35;{TrixEditorHelper.id+=1}”
选项[:类]| |=“trix内容”
选项[:数据]| |={}
选项[:数据][:直接上传\u url]=main\u app.rails\u直接上传\u url
选项[:data][:blob\u url\u template]=主应用程序.rails\u服务\u blob\u url(“:signed\u id”,“:filename”)
编辑器标记=内容标记(“trix编辑器”,“选项”)
输入\标记=隐藏\字段\标记(名称、值、id:选项[:输入])
输入标签+编辑器标签
结束
结束
模块ActionView::Helpers
类标记::TrixEditor
然后,我们从cdn安装了trix 1.1版,并使用标准的trix-attachments.js和直接上传


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

是否有
ActionTextRichText
型号可用?如果是这样,也许您可以将
has\u paper\u trail
添加到it@mr_sudaca该模型不可用。我尝试过,但现在我甚至无法运行
rails服务器
,因为我会得到:
…gems/activerecord-5.2.2/lib/active\u record/dynamic\u matchers.rb:22:in'method\u missing':未定义的方法'has\u many\u attached'for#(nomethoderor)
尝试在
app/models/action\u text/rich\u text.rb中创建并删除它。我会得到:
自动加载常量ActionText::RichText时检测到循环依赖项
您可以尝试将其放在初始化器中,并将其包装在
ActiveSupport中。加载(:active\u storage\u blob)时做。。。结束
不起作用。新版本(更新或创建)未保存到
版本
表中。您是如何处理ActiveStorage附件部分的?具体哪里需要它?我在安装了action\u text的型号中需要它
具有丰富的\u text
,但我认为,在应用程序_controller.rb或其他更全局的程序中也可能需要它。也许您还可以将此代码放入
config/initializers/
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