【问题标题】:Rails partial not rendering with JavaScriptRails 部分不使用 JavaScript 渲染
【发布时间】:2023-04-22 14:43:01
【问题描述】:

我正在尝试在我的应用程序中渲染部分内容,但由于某种原因它不会渲染。我认为这与我的资产管道以及我没有正确实现我想在我的部分中使用的 JavaScript 的事实有关。带有简单句子的测试部分就可以了。有人可以指导我在我的应用中正确使用 JavaScript 吗?

这是我想要展示的 jsFiddle:http://jsfiddle.net/yZQg4/

有问题的部分:

<%= javascript_include_tag "highcharts", "exporting", "jquery-1.4.2.min", "rails" %>

<script type="text/javascript" charset="utf-8">
  $(function() {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'panel_contents',
                type: 'column'
            },

            xAxis: {
                categories: ['Automotive', 'Agency', 'Contractor', 'Country Club', 'Other']
            },
            yAxis: {
                min: 0,

                title: {
                    text: 'Business Summary'
                },
                stackLabels: {
                    enabled: true,
                    style: {
                        fontWeight: 'bold',
                        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                    }
                }
            },
            legend: {
                align: 'right',
                x: 100,
                verticalAlign: 'top',
                y: 0,
                floating: true,
                backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
                borderColor: '#CCC',
                borderWidth: 1,
                shadow: false
            },
            tooltip: {
                formatter: function() {
                    return '<b>' + this.x + '</b><br/>' + this.series.name + ': ' + this.y + '<br/>' + 'Total: ' + this.point.stackTotal;
                }
            },
            plotOptions: {
                column: {
                    stacking: 'normal',
                    dataLabels: {
                        enabled: true,
                        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
                    }
                }
            },
            series: [{
                name: 'Mobile',
                data: [5, 3, 4, 27, 2]},
            {
                name: 'Foursquare',
                data: [2, 2, 3, 2, 1]},
            {
                name: 'Facebook',
                data: [3, 4, 4, 2, 5]},
            {
                name: 'Yelp',
                data: [3, 4, 4, 2, 5]},
            {
                name: 'Google',
                data: [3, 4, 4, 2, 5]}]
        });
    });

});​​
</script>

工作测试部分:

<h1> hello world </h1>

谢谢!

【问题讨论】:

  • 检查页面时控制台中是否有 JS 错误?

标签: ruby-on-rails ruby-on-rails-3 highcharts activeadmin partial


【解决方案1】:

在javascript代码之前添加:

<div id="panel_contents"></div>

附:此外,最好将javascript包含移出部分或使用content_for

【讨论】:

  • 我以前有过,但由于某种原因我删除了它。我只是包含它,但部分仍然没有出现。
  • 将 js 包含到您的布局中。 Firebug (Firefox) 或 Chrome 控制台是否会给您任何错误?
  • 我移动了它,并使用 Chrome 控制台进行了检查。出现此错误 - Uncaught SyntaxError: Unexpected token ILLEGAL
  • 好的,我用 Firebug 运行了完整的堆栈跟踪。这就是它所说的Illegal character,它指向我的 JavaScript 的最后一行。
【解决方案2】:

我刚刚在 TextEdit 中重新创建了该文件,它工作正常。出于某种原因,TextMate 偷偷将其添加到文件中:&amp;#8203;

【讨论】:

    最近更新 更多