簡體   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