You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Capistrano部署Rails应用时预编译资产找不到trix CSS文件

解决Rails 7.0.4.3部署时assets:precompile找不到trix CSS的问题
  • 确认服务器环境依赖
    部署服务器必须安装与本地一致版本的Node.js和Yarn,执行以下命令验证:

    node -v
    yarn -v
    

    若未安装,先安装对应版本。

  • 配置Capistrano自动执行Yarn安装
    检查Capfile是否引入了capistrano/yarn:

    # Capfile
    require 'capistrano/yarn'
    

    再在deploy.rb中添加Yarn配置,确保部署时自动安装依赖:

    set :yarn_flags, '--production --frozen-lockfile'
    set :yarn_roles, :all
    
  • 让Sprockets识别node_modules路径
    在config/initializers/assets.rb中添加node_modules到资产搜索路径:

    Rails.application.config.assets.paths << Rails.root.join('node_modules')
    

    同时确保样式文件正确引入trix CSS:

    • 若使用CSS Bundling(如Tailwind),在app/assets/stylesheets/application.tailwind.css中添加:
      @import "trix/dist/trix.css";
      
    • 若使用传统Sprockets,在app/assets/stylesheets/application.css中添加:
      *= require trix/dist/trix
      
  • 手动在服务器上排查编译问题
    登录服务器,进入当前部署目录,手动执行资产预编译,定位具体问题:

    cd /home/deploy/dynamic_performance/current
    bundle exec rake assets:precompile RAILS_ENV=production
    

    若仍报错,检查node_modules/trix/dist是否存在,不存在则执行yarn install后重试。

  • 验证Action Text配置
    确保app/javascript/controllers/actiontext.js存在,且在application.js中正确导入:

    import "@rails/actiontext"
    

    若使用Importmap,检查config/importmap.rb是否包含trix的映射:

    pin "trix", to: "trix/dist/trix.js", preload: true
    pin "@rails/actiontext", to: "actiontext.js", preload: true
    
  • 清理旧资产与缓存
    执行以下命令清理服务器上的旧资产和缓存,再重新预编译:

    bundle exec rake assets:clobber RAILS_ENV=production
    rm -rf tmp/cache/assets
    bundle exec rake assets:precompile RAILS_ENV=production
    

内容的提问来源于stack exchange,提问作者CJG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 14:33:13