Ruby on Rails js坏了

时间:2015-10-27 18:25:25

标签: javascript ruby-on-rails ruby node.js windows

我最近回到了Ruby,它终于为我工作了。也就是说,直到我尝试包含js。

此错误始终显示,并且首先删除提及的行

Showing C:/Users/1/2/app/views/layouts/application.html.erb where line #6 raised:
SyntaxError: [stdin]:1:18: reserved word "function"
Rails.root: C:/Users/1/2

但这并没有解决问题,我需要在我的项目中使用javascript。 我试过改变

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>

然后javascript无法正常工作

我已尝试安装节点,节点正常工作。 Javascript仍然没有。 我试过弄乱我的runtimes.rb

    JScript = ExternalRuntime.new(
 :name        => "JScript",
 :command     => "cscript //E:jscript //Nologo",
 :runner_path => ExecJS.root + "/support/jscript_runner.js",
 :encoding    => 'UTF-8' # CScript with //U returns UTF-16LE
)

我已经尝试了

gem 'coffee-script-source', '1.8.0'
gem 'therubyracer', platforms: :ruby

仍然没有

我真的希望nodejs有所帮助,但它没有。

的application.js:

// This is a manifest file that'll be compiled into application.js, which        will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts,    vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced    here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

welcome.coffee:

$(window).scroll(function() {
  if ($(this).scrollTop() > 1){
   $('nav').addClass("sticky");
   $('#title').addClass("sticky");
   $('a').addClass("sticky");
   $('input').addClass("sticky");
  }
  else{
    $('nav').removeClass("sticky");
    $('#title').removeClass("sticky");
    $('a').removeClass("sticky");
    $('input').removeClass("sticky");
  }
});
alert('Hello, World!');

Application.html.erb

<!DOCTYPE html>
<html>
 <head>
  <title>App title</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
 </head>
 <body>

  <%= yield %>

 </body>
</html>

welcome_controller.rb

class WelcomeController < ApplicationController
  def index
  end
end

1 个答案:

答案 0 :(得分:0)

在welcome.coffee中,这不是coffeescript。这是咖啡因的等价物:

$(window).scroll ->
  if $(this).scrollTop() > 1
    $('nav').addClass 'sticky'
    $('#title').addClass 'sticky'
    $('a').addClass 'sticky'
    $('input').addClass 'sticky'
  else
    $('nav').removeClass 'sticky'
    $('#title').removeClass 'sticky'
    $('a').removeClass 'sticky'
    $('input').removeClass 'sticky'
  return
alert 'Hello, World!'

你有javascript它期待coffeescript。它抱怨,因为它无法解析文件的语法错误,说函数是一个javascript键。 Coffeescript编译器没有像。