完美的选择,在我使用 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