coffeescript-我们可以在rails上的ruby的view文件夹内使用coffeescript代码吗?

时间:2018-12-04 10:32:44

标签: javascript ruby-on-rails coffeescript

我想在我的views文件夹中使用CoffeeScript。由于我的视图文件夹中有一个_test.html.erb的部分文件,如下所示:

<canvas id="doughnutChart" width="250" height="250"></canvas>

<script type="text/javascript">
  var doughnut = document.getElementById("doughnutChart").getContext("2d");
  var dchart = new Chart(doughnut, {
      type: 'doughnut',
      data: {
      },
      options:{
      }
  });
</script>

现在,我想在这里使用CoffeeScript,我该怎么做。

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用CDN coffee script cdn

或者您可以访问我的js小提琴 Inline cofeescript inside html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8"/>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/coffee-script/1.7.1/coffee-script.min.js"></script>
        <title>CoffeScript on browser</title>
      </head>
      <body>
        <canvas id="myChart"></canvas>

        <script type="text/coffeescript">
          alert 'It works!'
          ctx = document.getElementById('myChart').getContext('2d')
          chart = new Chart(ctx,
            type: 'bar'
            data:
              labels: [
                'January'
                'February'
                'March'
                'April'
                'May'
                'June'
                'July'
              ]
              datasets: [ {
                label: 'My First dataset'
                backgroundColor: 'rgb(255, 99, 132)'
                borderColor: 'rgb(255, 99, 132)'
                data: [
                  0
                  10
                  5
                  2
                  20
                  30
                  45
                ]
              } ]
            options: {})
        </script>
      </body>
    </html>