Rails-通过资产管道加载资产

传奇

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

  1. 我将它们全部移至应用程序/资产,为JS和CSS call添加了清单文件//= 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.js我进入的资产,我include_tag尝试将其删除并再次尝试没有任何骰子。

我尝试了bundle exec rake assets:cleanbundle exec rake assets:precompile,两者都没有问题。自从为资产添加宝石以来就捆绑了,并且每次都重新启动战俘。

我不确定我是否经历了所有错误,或者我错过了一步?任何以前经历过此操作的人,都将不胜感激提示或线索。

迪伦·马科(Dylan Markow)

您说您删除了所有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文件,都必须在出现更改之前重新编译资产。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章