【问题标题】:Rails: How to implement Action Text in Rails 5.2.1?Rails:如何在 Rails 5.2.1 中实现动作文本?
【发布时间】:2019-05-17 01:16:16
【问题描述】:

我在将Action Text gem 实施到我的Rails 5.2.1 应用程序时遇到了困难。

我按照安装指南进行操作,富文本编辑器不会出现在我的 _form 视图中。

然后我意识到它需要带有 webpacker 和 主动存储 的 Rails 6。更改我的 gemfile 的 Rails 版本并运行 rails upgrade 不起作用,所以我添加了 webpacker 和活动存储 gem,并将它与我当前的 5.2.1 版本捆绑在一起。那也没用。

我真的很想在我的应用程序中使用富文本编辑器。它不是必须的 Trix 编辑器,但由于它将是 v6 的原生版本,我认为这是显而易见的选择。

我的源代码参考

【问题讨论】:

    标签: ruby-on-rails ruby wysiwyg rich-text-editor trix


    【解决方案1】:

    完美的选择,在我使用 rails 5.2.3 的新应用程序上进行了测试。 请不要操作文本需要 ruby​​ 2.5.3 版本。 首先:添加 webpacker

    您可以在设置新的 Rails 5.1+ 应用程序期间使用新的 --webpack 选项添加 Webpacker:

    可用的 Rails 5.1+

    rails new myapp --webpack
    

    或将其添加到您的 Gemfile:

    宝石文件

    gem 'webpacker', '~> 4.x'
    

    或者如果你更喜欢使用 master

    gem 'webpacker', git: 'https://github.com/rails/webpacker.git'
    yarn add https://github.com/rails/webpacker.git
    

    现在使用图像魔法将 Actiontext 添加到您的 gem 文件中:

    gem'actiontext',github:'kobaltz/actiontext',branch:'archive',require:'action_text'
    gem 'image_processing'
    

    最后,运行以下命令安装 Webpacker:

    bundle
    bundle exec rails webpacker:install
    
    rails action_text:install
    rails db:migrate
    brew install imagemagick vips
    

    将此添加到您的视图/布局/应用程序的头部

    #layouts/application.html.erb
    <%= javascript_pack_tag 'application' %>
    

    在您的模型中,它可能是一篇文章或您模型中的任何内容

    class Article < ApplicationRecord
      has_rich_text :content
    end
    

    在你的控制器中

     #controllers/articles_controller.rb
        def article_params
          params.require(:article).permit(:name, :content)
        end
    

    在你的表格中

    #_form.html.erb
    <div class="field">
      <%= form.label :content %>
      <%= form.rich_text_area :content %>
    </div>
    

    最后在你的视图中显示内容;

    <h1><%= @article.name %></h1>
    <p><%= @article.content %></p>
    

    可选:修复“未满足的对等依赖”警告,

    yarn upgrade
    

    您可以在此处观看完整视频: https://www.driftingruby.com/episodes/using-action-text-in-a-rails-5-2-application

    【讨论】:

    【解决方案2】:

    我有部分答案给你!我想使用 Trix、Rails 5.2.2 和 Webpacker,但找不到明确的答案。 trix gem 不起作用,因为我根本不使用资产管道。 (我也使用 Turbolinks 和 Stimulus,到目前为止,这与它们一起工作得很好。)

    我使用以下方法将解决方案串在一起:

    我现在可以使用如下所示的表单助手(使用 Slim 模板语言):

    = f.trix :personal_notes, class: 'some-css-class'
    

    为了到达那里,我做了以下事情:

    1. yarn add trix
    2. 在我的 Javascript 包中,import 'trix';
    3. 在我的 CSS 包中,@import '~trix/dist/trix';
    4. 然后我创建了表单助手。看起来是这样的:

    在app/helpers/application_helper.rb 中,我添加了新的表单助手,基于我在上面链接的那个 Stack 帖子。我的文件现在看起来像这样:

    module ApplicationHelper
        # ...
    
        class ActionView::Helpers::FormBuilder
            # include ActionView::Helpers::TagHelper # I didn't need this.
            include ActionView::Helpers::FormTagHelper
            include ActionView::Helpers::FormOptionsHelper
            # include ActionView::Helpers::CaptureHelper  # I didn't need this.
            # include ActionView::Helpers::AssetTagHelper  # I didn't need this.
    
            # since these tag helpers just return strings, it is easy to make a pretty 
            # powerful helper.  It is probably possible to use existing Rails form 
            # helpers to aid this as well.
            def trix(method, options = {})
              value = (@object[method].empty?) ? '' : "value='#{@object[method]}'" 
              return "<input id='#{field_id(method)}' type='hidden' name='#{field_name(method)}' #{value}><trix-editor input='#{field_id(method)}' class='trix-content #{options[:class]}'></trix-editor>".html_safe
            end
    
            def field_name(label,index=nil)
              return @object_name + "[#{label}]"
            end
    
            def field_id(label,index=nil)
              return @object_name + "_#{label}"
            end
    
        end
    end
    

    重启 Rails。似乎对我有用。

    【讨论】:

    • 非常感谢您,Mungojerie。我一定会考虑的。我能问你为什么选择不使用资产管道吗?
    • 部分是因为 Rails 正在转向 webpack,部分是因为我已经习惯了,部分是因为模块热重载。因此,考虑到这一点,使用 JUST webpack 对我的大脑来说要容易得多……所有 JS、CSS 和图像都由一个进程处理。
    猜你喜欢
    • 2019-04-16
    • 2013-01-27
    • 2013-06-18
    • 1970-01-01
    • 2019-09-23
    • 2019-03-31
    • 2016-05-26
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多