繁体   English   中英

Ruby on Rails js已损坏

[英]Ruby on Rails js is broken

我最近又回到了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

在您的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!'

您在需要coffeescript的地方使用了javascript。 它抱怨是因为它无法解析语法错误的文件,并说函数是javascript键对。 Coffeescript编译器没有可能。

暂无
暂无

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

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