javascript - 类型错误:评论未定义
问题描述
我想在生产环境中运行的 Rails 5.1.4 应用程序中使用 JavaScript 和 rails-ujs 通过 AJAX 提交新评论,数据库 Postgres 11(没有部署)
(这适用于开发环境)
但是当我尝试发表新评论时,我的观点并没有改变,我web-console
收到了这个:
TypeError: Comments is undefined
在我的 create.js.erb 文件中引用第 9 行
创建.js.erb
var comment = {
'body': '<%= @comment.body %>',
'commenter': '<%= @comment.user.name %> ',
'commenterId': '<%= @comment.user.id %> ',
'avatar': '<%= @comment.user.avatar %> ',
'datetime': '<%= @comment.created_at.strftime('on %e %b %Y at %H:%M') %>',
};
Comments.displayComment(comment); # <~ Line causing the error
console.log("Comment", comment);
提取的comments.js
Comments = {};
Comments.displayComment = function(comment) {
var commentBlock = document.createElement('blockquote');
commentBlock.className = 'blockquote';
...
...
...
var comments = document.getElementById('commentList');
comments.appendChild(commentBlock);
};
提取的显示视图(设置远程:true)
<%= form_for([@tip, @tip.comments.build], remote: true) do |form| %>
我一直在查看其他帖子以寻找类似的答案,但还没有运气。另外,我尝试在create.js.erb
文件中声明一个变量“评论”,但收到了同样的错误。
我想我在我的 javascript 中遗漏了一些东西,但我不确定是什么。任何帮助,将不胜感激。谢谢。
解决方案
这些是我解决它的步骤:
在我的中,我发现与文件production.log
有冲突。bootstrap.js
所以,我提前删除了它。
- 删除文件
public/assets
夹中的所有文件。(删除之前的预编译) - 清空
/my_rails_app/tmp
文件夹。(清除由 rails 创建的临时文件) - 运行
RAILS_ENV=production rails assets:precompile
。(预编译我的资产管道) -
RAILS_ENV=production rails server
在您的Terminal
(bash/shell
)上运行。或者只是运行./run-my-production.sh
文件(在生产环境中重新启动服务器)。这里是run-production.sh文件的示例 - 访问您的https://localhost:3000 并重试:我的 AJAX 调用按预期正常工作。
干杯。
推荐阅读
- android - 如何更改android切换拇指区域的颜色?
- javascript - 使用参数获取 django 视图的 url
- flutter - 如何将窗口设备与运行在虚拟机上的 android studio / vscode 连接以实现颤振桌面应用程序?
- haskell - 使用 RecordDotSyntax 更新记录会导致错误
- python - cv2 ImportError(原因:找不到图像)
- swift - 如何使用父 id 来放置孩子的数据?
- express - 有条件地放宽 CSP 策略或放宽用于显示 iframe 内容的特定 Nest 路由的 CSP - Node/NestJS
- c# - 如何修复 ASP.NET MVC 用户界面应用错误的输入。ASP.NET 标识
- postgresql - 按临时变量排序时加快 ORDER BY 速度?
- swift - Swiftui - 无法访问环境对象