Rails ActionText 读取时会自动加上 布局模板

canonpd · January 28, 2020 · Last by canonpd replied at February 02, 2020 · 2948 hits

我的项目使用了 ActionText,但我没用 Trix 编辑器,我的编辑器是我参照 Github,自己定制 Markdown 编辑器。

现在有一个问题,当我做编辑的时候,使用如下代码

<%= form.text_area :body %>

他会自动渲染这个模板 actiontext-6.0.2.1/app/views/action_text/content/_layout.html.erb

<div class="trix-content">
  <%= render_action_text_content(content) %>
</div>

导致我读出来的内容,全部都有了多余的标签。

@huacnlee 请问下,在做 Bluedoc 的时候,你是否也碰到了这个问题,是怎么解决的?

查了源码,找到两个方法能读取原始数据

@article.body.to_plain_text

@article.body.body.to_html

不知有没有更好的解决方案,暂时先这样

<%= form.text_area :body, value: @article.body.to_plain_text %>
Reply to huacnlee

非常感谢,已经换上了。😀 ,就是想用他的表结构和提供的一些便利方法,你的包正适合我的需求使用。

又看了 bluedoc 的源码,是说 gemfile 中怎么没看到用其他包,读取的时候又没看到怎么特别处理,原来是直接集成在项目中。原谅我,bluedoc 的源码规模太庞了,没有细看。

另外还有两个小问题:

  • 1. 现在推荐用 Active Storage 还是 CarrierWave ?我会用阿里云 OSS,所以不考虑本地存储问题
  • 2. 我注意到 homeland 源码,做了 bundle update,gemfile.lock 中使用的是 rack (2.1.1)。这会导致 sidekiq,后台的 web 界面崩掉,相关讨论在此https://github.com/mperham/sidekiq/issues/4422。我个人是指定到 2.0.8 版本解决了这个问题。
Reply to canonpd

Active Storage +1

5 Floor has deleted

@huacnlee

actiontext-lite 删除文章后,action_text_rich_texts 表对应数据未自动删除。

- has_one :"rich_text_#{name}", -> { where(name: name) }, class_name: "ActionTextLite::RichText", as: :record, inverse_of: :record 

直接抄 ActionText gem 包中源码如下

+ has_one :"rich_text_#{name}", -> { where(name: name) },
                class_name: "ActionTextLite::RichText", as: :record, inverse_of: :record, autosave: true, dependent: :destroy

已提交 pull requests:https://github.com/huacnlee/actiontext-lite/pull/1

烦请通过

You need to Sign in before reply, if you don't have an account, please Sign up first.