简体   繁体   English

Rails部分无法使用JavaScript渲染

[英]Rails partial not rendering with JavaScript

I'm trying to render a partial in my app, but for some reason it will not render. 我正在尝试在我的应用程序中渲染部分图像,但是由于某种原因它不会渲染。 I think it has to do with my asset pipeline and the fact that I am not correctly implementing the JavaScript I want to use in my partial. 我认为这与资产管道以及我未正确实现要在我的局部代码中使用的JavaScript有关。 A test partial with a simple sentence works just fine. 一个带有简单句子的测试部分效果很好。 Can someone direct me in the proper use of JavaScript in my app? 有人可以指导我正确使用我的应用程序中的JavaScript吗?

Here is a jsFiddle of what I am trying to show: http://jsfiddle.net/yZQg4/ 这是我要显示的内容的jsFiddle: http : //jsfiddle.net/yZQg4/

Problematic partial: 有问题的部分:

<%= 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>

Working test partial: 工作测试局部:

<h1> hello world </h1>

Thanks! 谢谢!

Add before the javascript code this: 在javascript代码之前添加以下代码:

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

PS Besides it's better move javascript inclusion out of partial or using a content_for PS此外,最好将javascript包含移出部分内容或使用content_for

I just recreated the file in TextEdit, and it worked. 我只是在TextEdit中重新创建了文件,并且它起作用了。 For some reason TextMate was secretly adding this to the file: &#8203; 由于某种原因,TextMate秘密将其添加到文件中: &#8203; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM