简体   繁体   中英

Ruby on Rails js is broken

I've recently gotten back into Ruby and its finally working for me. That is, until I try to include 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. 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

I've tried installing node, and node works. Javascript still doesn't. I've tried messing with my 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.

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

in your welcome.coffee, that's not coffeescript. Here is the coffeescript equivalent:

$(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. It was complaining because it failed to parse the file with a syntax error saying function is a javascript keyward. Coffeescript compiler no likey.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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