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

Ruby on Rails配置Trix/Action Text时报application未定义错误

修复方案

  1. 修正application.html.erb中的资源引入标签
    你当前的代码错误使用了javascript_tag,该标签用于直接嵌入JavaScript代码,而非引入外部编译后的JS文件,同时重复引入JS资源会导致加载冲突,修改为以下内容即可:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  1. 清理application.js的重复导入逻辑
    你同时混用了Sprockets注释导入语法和ES模块导入语法,还重复加载了多次trix和actiontext,会导致运行冲突,修改为如下内容:
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
import Rails from "@rails/ujs";
import Turbolinks from "turbolinks";
import * as ActiveStorage from "@rails/activestorage";
import "channels";
import "trix";
import "@rails/actiontext";
import "bootstrap";
import "jquery_ujs";

Rails.start();
Turbolinks.start();
ActiveStorage.start();
  1. 修复资源加载404问题
    执行以下命令重新安装依赖、编译静态资源:
yarn add trix @rails/actiontext
rails assets:clobber assets:precompile
  1. 确认模型配置
    确保你的Article、Comment模型都配置了Action Text关联:
class Article < ApplicationRecord
  has_rich_text :body
  # 其余原有逻辑保留
end

class Comment < ApplicationRecord
  # 字段名替换为你实际使用的评论富文本字段名
  has_rich_text :content
  # 其余原有逻辑保留
end

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 01:57:02