Rails - 通过Asset Pipeline加载资产

时间:2013-08-08 12:48:55

标签: ruby-on-rails asset-pipeline

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

  1. 我将它们全部移动到app / assets,我添加了JS和CSS调用//= require_tree .的清单文件。

  2. 添加了以下宝石

    group :assets do
      gem 'coffee-rails'
      gem 'uglifier'
      gem 'sass-rails'
      gem 'therubyracer'
    end
    
  3. 删除了除javascript_include_tags

  4. 以外的所有= javascript_include_tag 'https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js'
  5. 在我的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'
    
  6. 在config / environments / development.rb文件集中

    # Do not compress assets
    config.assets.compress = false
    
    # Expands the lines which load the assets
    config.assets.debug = true
    
  7. 我已添加到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 )
    
  8. 我重新阅读Asset Pipeline上的部分只是为了看看我是否搞砸了什么。但到目前为止,没有任何资产从app/assets开始,唯一正在运作的是jquery.min.js我通过include_tag进入我尝试删除它并再次尝试没有骰子

    我尝试了bundle exec rake assets:cleanbundle exec rake assets:precompile,两者都没有问题。自从为资产添加宝石后捆绑了,每次都重启pow。

    我不确定我是否经历过这一切,或者我错过了一步?任何人经历过此之前和提示或线索将非常感激。

1 个答案:

答案 0 :(得分: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文件,您都必须在更改显示之前重新编译资产。