[英]Ruby on Rails js is broken
I've recently gotten back into Ruby and its finally working for me. 我最近又回到了Ruby,它终于为我工作。 That is, until I try to include js. 也就是说,直到我尝试包括js。
This error shows up all the time, and at first deleting the line mentioned worked 此错误一直显示,首先删除提到的行即可
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
But that doesn't fix the issue, and I need to use javascript in my project. 但这不能解决问题,我需要在项目中使用javascript。 I've tried changing 我尝试改变
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
to 至
<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
but then javascript doesn't work 但是JavaScript不起作用
I've tried installing node, and node works. 我尝试安装节点,并且节点可以工作。 Javascript still doesn't. Javascript仍然没有。 I've tried messing with my runtimes.rb 我试图弄乱我的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
)
I've tried 我试过了
gem 'coffee-script-source', '1.8.0'
gem 'therubyracer', platforms: :ruby
And still nothing 还是一无所有
I was really hoping nodejs would help but it didn't. 我真的希望nodejs会有所帮助,但没有成功。
Application.js: 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: 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 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 welcome_controller.rb
class WelcomeController < ApplicationController
def index
end
end
in your welcome.coffee, that's not coffeescript. 在您的welcome.coffee中,这不是咖啡脚本。 Here is the coffeescript equivalent: 这是等效的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!'
You had javascript where it was expecting coffeescript. 您在需要coffeescript的地方使用了javascript。 It was complaining because it failed to parse the file with a syntax error saying function is a javascript keyward. 它抱怨是因为它无法解析语法错误的文件,并说函数是javascript键对。 Coffeescript compiler no likey. Coffeescript编译器没有可能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.