0

我正在尝试在我拥有的旧 Rails 应用程序上设置资产管道,以便我可以开始使用 CoffeeScript。我正在使用 ruby​​ 1.9.3-p327 和 Rails 3.2.13。我曾经把我所有的 JS、CSS 和我的图片都存放在public/文件夹中。这就是我到目前为止所做的->

  1. 我将它们全部移至应用程序/资产,我为 JS 和 CSS 调用添加了清单文件//= require_tree .

  2. 添加了以下宝石

    group :assets do
      gem 'coffee-rails'
      gem 'uglifier'
      gem 'sass-rails'
      gem 'therubyracer'
    end
    
  3. 删除了我的所有javascript_include_tags除了= javascript_include_tag 'https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js'

  4. 将以下内容添加到我的config/application.rb文件中

    # Enforce whitelist mode for mass assignment.
    # This will create an empty whitelist of attributes available for mass-assignment for all models
    # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
    # parameters by using an attr_accessible or attr_protected declaration.
    config.active_record.whitelist_attributes = true
    
    # 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'
    
  5. 在 config/environments/development.rb 文件集中

    # Do not compress assets
    config.assets.compress = false
    
    # Expands the lines which load the assets
    config.assets.debug = true
    
  6. 我添加到 config/environments/production.rb

    # Compress JavaScripts and CSS
    config.assets.compress = true
    
    # Choose the compressors to use
    # config.assets.js_compressor  = :uglifier
    # config.assets.css_compressor = :yui
    
    # Don't fallback to assets pipeline if a precompiled asset is missed
    config.assets.compile = false
    
    # Generate digests for assets URLs.
    config.assets.digest = true
    
    # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
    # config.assets.precompile += %w( search.js )
    

我重新阅读了资产管道上的部分,只是为了看看我是否搞砸了。但到目前为止,没有任何资产从app/assets唯一有效的资产中拉出来jquery.min.jsinclude_tag我已经尝试移除它并再次尝试不掷骰子。

我试过bundle exec rake assets:clean, 和bundle exec rake assets:precompile, 两者都运行没有问题。并在为资产添加 gem 后捆绑,并且每次都重新启动 pow。

我不确定我是否正在经历这一切都错了,或者我错过了一步?任何人以前经历过这个和提示或线索将不胜感激。

4

1 回答 1

4

你说你删除了所有的javascript_include_tag陈述。假设您将清单命名为app/assets/javascripts/application.jsapp/assets/stylesheets/application.css,您需要将它们包含在您的布局中:

<%= stylesheet_link_tag    "application", :media => "all" %>
<%= javascript_include_tag "application" %>

否则,Rails 将不知道加载清单文件

此外,您不应该rake assets:precompile在开发中使用。如果这样做,那么无论何时更改 JS/CSS 文件,都必须在更改显示之前重新编译资源。

于 2013-08-08T13:00:05.850 回答