Rails + Javascript + Uncaught TypeError: Object #<Object> has no method

Posted

技术标签:

【中文标题】Rails + Javascript + Uncaught TypeError: Object #<Object> has no method【英文标题】: 【发布时间】:2013-03-13 01:40:09 【问题描述】:

我正在使用我购买的模板,但遇到了这个问题:

Uncaught TypeError: Object #<Object> has no method 'peity' in unicorn.dashboard.js:11

这是第 11 行:

unicorn.peity();

在我的清单文件中,我得到了这个:

//= require admins/cms/excanvas.min
//= require admins/cms/jquery.min
//= require admins/cms/jquery.ui.custom
//= require admins/cms/jquery.uniform
//= require admins/cms/jquery.flot.min
//= require admins/cms/jquery.flot.resize.min
//= require admins/cms/jquery.dataTables.min
//= require admins/cms/jquery.gritter.min
//= require admins/cms/jquery.validate

//= require admins/cms/jquery.peity

//= require admins/cms/jquery.wizard
//= require admins/cms/select2.min
//= require admins/cms/fullcalendar.min
//= require admins/cms/bootstrap.min
//= require admins/cms/bootstrap-colorpicker
//= require admins/cms/bootstrap-datepicker
//= require admins/cms/unicorn

//= require admins/cms/unicorn.dashboard

//= require admins/cms/unicorn.form_common
//= require admins/cms/unicorn.form_validation
//= require admins/cms/unicorn.wizard
//= require admins/cms/unicorn.interface
//= require admins/cms/unicorn.tables
//= require admins/cms/unicorn.charts
//= require admins/cms/unicorn.calendar

这是在模板附带的静态 index.html 中加载脚本的方式:

<script src="js/jquery.min.js"></script>
<script src="js/jquery.ui.custom.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap-colorpicker.js"></script>
<script src="js/bootstrap-datepicker.js"></script>
<script src="js/jquery.uniform.js"></script>
<script src="js/select2.min.js"></script>
<script src="js/unicorn.js"></script>
<script src="js/unicorn.form_common.js"></script>

这就是我的仪表板索引脚本的加载方式:

<script src="/assets/admins/cms/excanvas.min.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/jquery.min.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/jquery.ui.custom.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/jquery.uniform.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/jquery.flot.min.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/jquery.flot.resize.min.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/jquery.dataTables.min.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/jquery.gritter.min.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/jquery.validate.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/jquery.peity.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/jquery.wizard.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/select2.min.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/fullcalendar.min.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/bootstrap.min.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/bootstrap-colorpicker.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/bootstrap-datepicker.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/unicorn.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/unicorn.dashboard.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/unicorn.form_common.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/unicorn.form_validation.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/unicorn.wizard.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/unicorn.interface.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/unicorn.tables.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/unicorn.charts.js?body=1" type="text/javascript"></script>
<script src="/assets/admins/cms/unicorn.calendar.js?body=1" type="text/javascript"></script>

奇怪的是 unicorn.dashboard 是在 jquery.peity 之后加载的。另一个额外的事情是,当我打开模板附带的 index.html 时,它根本没有收到此错误。我不确定问题可能是什么。我已经尝试了几个小时来解决这个问题,但我一点运气都没有。

如果有人可以帮助我解决这个问题,那将非常有帮助。提前致谢。

【问题讨论】:

你能发布在普通 index.html 中有效的 JavaScript 引用吗?我怀疑您的资产管道中缺少某些内容,或者正在使用不同版本的文件。此外,删除独角兽需要并插入您自己的 console.log($("body").peity) 以确保实际加载了 peity 可能很有用。 你是丹,我更新了我的评论。在完成 console.log($"body").peity) 之后,我在 chrome 控制台的最后一行得到了一个函数和未定义的函数。 【参考方案1】:

如果您(临时)更改您的清单以完全匹配 index.html 文件,那么一切正常吗?

如果是这样,我怀疑 peity、独角兽和其他包含的东西(可能是 explorecanvas)之间存在冲突。

【讨论】:

以上是关于Rails + Javascript + Uncaught TypeError: Object #<Object> has no method的主要内容,如果未能解决你的问题,请参考以下文章

Rails:在 Rails 视图中使用 javascript 函数的正确语法

(Ruby,Rails,Javascript)禁用Javascript时重定向/呈现不同的页面......?

Rails - 找不到 JavaScript 运行时?

Ember.js + Leaflet + rails(使用 Javascript MVC 创建 Rails 应用程序并打开源地图)

Rails - 将视图的 javascript 代码移动到 javascript 文件

在 Javascript 中使用 Rails 关联