简体   繁体   中英

Rails 3.1 - Coffescript doesn't compile to js

I am migrating app from Rails 3.0 to Rails 3.1. I changed all nessasary config files and installed gems (with help of Ryan's screencast ). I have few layouts and i want to load different scripts for them.

I have view:

layouts/console.html.erb

<html lang="ru">
  <head>
    ...
    <%= stylesheet_link_tag "console" %>
    <%= yield :stylesheets%>
    <%= javascript_include_tag 'console' %>
    ...
    <%= yield :javascripts%>
  </head>
</html>

In my assets i created file console.js:

assets/javascripts/console.js

// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_self
//= require_tree ./console

Also i created folder console with file inside of this folder:

assets/javascripts/console/users.js.coffee

jQuery ->
  alert "Test"

But when i load such page alert doesn't appear.

When i debug page i see that Rails loaded this

<script type="text/javascript" src="/assets/console.js?body=1"></script>

Which contains:

// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
; 

And this:

<script type="text/javascript" src="/assets/console/users.js.coffee ?body=1"></script>

Which contains:

jQuery ->
alert "Test"
; 

What is my problem? How can i fix this?

UPD : links in the head of the page:

<head>
   ...
   <meta charset="utf-8">
   <meta name="csrf-param" content="authenticity_token">
   <meta name="csrf-token" content="PAO2QZ6Z3ykmksXnY55dmCehq+i2COXmSlnZWjErFwA=">
   <link type="image/vnd.microsoft.icon" rel="shortcut icon" href="/favicon.ico">
   <link type="text/css" rel="stylesheet" media="screen" href="/assets/console.css?body=1">
   <link type="text/css" rel="stylesheet" media="screen" href="/assets/console/console.css?body=1">
   <link type="text/css" rel="stylesheet" media="screen" href="/assets/console/pagination.css?body=1">
   <link type="text/css" rel="stylesheet" media="screen" href="/assets/console/search-form.css?body=1">

   <script type="text/javascript" src="/assets/jquery.js?body=1"></script>
   <script type="text/javascript" src="/assets/jquery_ujs.js?body=1"></script>
   <script type="text/javascript" src="/assets/console.js?body=1"></script>
   <script type="text/javascript" src="/assets/console/users.js.coffee ?body=1"></script>  
</head>

Updated Answer:

There is a space in line

<script type="text/javascript" src="/assets/console/users.js.coffee ?body=1"></script>

that shouldn't be there!

Make sure there aren't any trailing spaces in the file name of users.js.coffee .

Original answer: (Wasn't the solution)

It doesn't look like coffeescript has interpreted the line

jQuery ->

Are you loading jQuery correctly? Make sure you've got

gem 'jquery-rails'

in your Gemfile , and you've run bundle install

It may also be worth trying the alternate syntax of $ -> , although it shouldn't make a difference.

Rails 3.0 application may missing some code when running on Rails 3.1.

First, check if you have those gems in your Gemfile:

group :assets do
  gem 'jquery-rails'
  gem 'jquery-ui-rails'
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

Second, check if those lines are in config/application.rb:

module SAH2Client
 class Application < Rails::Application

    ......

    # Enable the asset pipeline
    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'
  end

end

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