TypeError: Comments is undefined
TypeError: Comments is undefined
我想在我的 Rails 5.1.4 应用 运行 中使用 JavaScript 和 rails-ujs 通过 AJAX 提交新评论生产环境,数据库Postgres 11(未部署)
(在开发环境下有效)
但是当我尝试 post 新评论时,我的观点没有改变,在我的 web-console
中我收到了这个:
TypeError: Comments is undefined
引用我的 create.js.erb 文件中的第 9 行
create.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| %>
我一直在检查其他 post 以寻找类似的答案,但还没有成功。
另外,我尝试在 create.js.erb
文件中声明一个变量 'Comments',但收到了同样的错误。
我想我的 javascript 中遗漏了一些东西,但我不确定是什么。
任何帮助,将不胜感激。
谢谢。
Dan,您的资产管道似乎在预编译您的资产时遇到了问题。我建议您在 运行 处于开发模式的应用程序查找 javascript 文件的错误时检查控制台中的输出。
然后再次在 运行 precompile
命令之前清空 tmp
文件夹。
帮我解决了问题。
这些是我解决它所遵循的步骤:
在我的 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 呼叫正常工作。
干杯。
我想在我的 Rails 5.1.4 应用 运行 中使用 JavaScript 和 rails-ujs 通过 AJAX 提交新评论生产环境,数据库Postgres 11(未部署)
(在开发环境下有效)
但是当我尝试 post 新评论时,我的观点没有改变,在我的 web-console
中我收到了这个:
TypeError: Comments is undefined
引用我的 create.js.erb 文件中的第 9 行
create.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| %>
我一直在检查其他 post 以寻找类似的答案,但还没有成功。
另外,我尝试在 create.js.erb
文件中声明一个变量 'Comments',但收到了同样的错误。
我想我的 javascript 中遗漏了一些东西,但我不确定是什么。 任何帮助,将不胜感激。 谢谢。
Dan,您的资产管道似乎在预编译您的资产时遇到了问题。我建议您在 运行 处于开发模式的应用程序查找 javascript 文件的错误时检查控制台中的输出。
然后再次在 运行 precompile
命令之前清空 tmp
文件夹。
帮我解决了问题。
这些是我解决它所遵循的步骤:
在我的 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 呼叫正常工作。
干杯。