Rails不包括javascript的require_tree

时间:2013-12-09 13:02:29

标签: javascript ruby-on-rails ruby coffeescript

在检查更复杂的javascript(coffee)代码无效后,我一直试图在我的rails应用程序中用javascript执行一个简单的控制台和警报消息。我能够打印的唯一方法是将代码放在application.js中。以下是我的应用程序中的主要文件:

的Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.0'

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'jquery-rails', '3.0.4'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'mongo_mapper'
gem 'haml', "~> 4.0.4"
gem 'i18n', '~> 0.6.9'

资产/ Javascript角/ application.js中

//= require jquery
//= require jquery_ujs
//= require_tree .

资产/ Javascript角/ example.coffee

$(document).ready(function() {
  console.log( "pRUEBAEFADF")
});

配置/ application.rb中

require File.expand_path('../boot', __FILE__)

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"

if defined?(Bundler)
  Bundler.require(*Rails.groups(:assets => %w(development test)))
end

module HotelAbadi
  class Application < Rails::Application
    config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    config.i18n.default_locale = :es

    config.encoding = "utf-8"

    config.filter_parameters += [:password]

    config.assets.enabled = true
    config.assets.paths << "#{ Rails.root }/app/assets/fonts"

    config.assets.version = '1.0'
  end
end

配置/环境/ development.rb

HotelAbadi::Application.configure do
  config.cache_classes = false
  config.whiny_nils = true
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  config.action_mailer.raise_delivery_errors = false
  config.active_support.deprecation = :log
  config.action_dispatch.best_standards_support = :builtin
  config.assets.compress = false
  config.assets.debug = true
  config.assets.compile = true
  config.serve_static_assets = false
end

我还试图手动添加文件,但它没有执行任何操作。我找不到更多关于这个问题的页面。提前谢谢。

编辑:我已将版本从3.2.0更新为3.2.15,问题解决了。

2 个答案:

答案 0 :(得分:2)

你的assets/javascripts/example.coffee不是Coffeescript,是Javascript。将文件重命名为assets/javascripts/example.js或将代码翻译为Coffeescript。此外,如果您希望将其翻译为Coffeescript,则名称应为assets/javascripts/example.js.coffee

答案 1 :(得分:-2)

添加'therubyracer'宝石非常重要。

在资产组中添加此行

gem 'therubyracer', :platforms => :ruby

此处提供更多信息:https://github.com/cowboyd/therubyracer