摘要路径 &asset_digest_path 不允许使用摘要 URL [英] digest_path & asset_digest_path not allowing digest URLs

查看:37
本文介绍了摘要路径 &asset_digest_path 不允许使用摘要 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制作资产方面一直处于非常艰难的时期.归结为我试图覆盖 sprokets 助手模块以尝试查看发生了什么......当我将其重写为以下内容时:

I have been having this pretty rough time with assets on production. It came down to me trying to override the sprokets helper module to try and see what was up... When I re-wrote it to the following:

module Sprockets
  module Rails
    module Helper
      def compute_asset_path(path, options = {})

它不会运行.我在本地环境中尝试过,运行完美.生产环境不允许我的资产以摘要形式呈现,但我的本地环境允许吗?这与这位绅士所问的有关...

It would not run. I tried it in my local environment, and it runs perfectly. Is there a reason the production environment would not allow my assets to render in digest form, but my local environment would? This is related to what this gent was asking...

Rails 4.0.3 使用 asset_sync 生成错误的资产路径

这是我尝试覆盖该方法后收到的错误(使用 RAILS_ENV=development 运行时没有错误):

Here is the error I receive after trying to override that method (which doesn't have an error when running with RAILS_ENV=development):

ActionView::Template::Error (undefined local variable or method `digest_path' for #<#<Class:0x000001034a99e0>:0x000001034a81d0>):

Gemfile 中的 Asset Gems 以供参考:

Asset Gems in Gemfile for reference:

source 'http://rubygems.org'
# ruby '2.1.1'

gem 'rails', '4.0.4'
gem 'jbuilder', '~> 1.2'
gem 'devise'
gem 'devise_invitable'
gem 'figaro'
gem 'mysql2'
gem 'simple_form'
gem 'kaminari'
gem 'statistics'
gem 'possessive'
gem 'geocoder'
gem 'nokogiri'
gem 'asset_sync'
gem 'sprockets-rails', :require => 'sprockets/railtie'
gem 'ledermann-rails-settings', :require => 'rails-settings'
gem 'public_activity'

group :assets do
  gem 'therubyracer'
  gem 'sass-rails', '~> 4.0.0'
  gem 'uglifier', '>= 1.3.0'
  gem 'coffee-rails', '~> 4.0.0'
end

group :development do
  gem 'better_errors'
  gem 'binding_of_caller', :platforms=>[:mri_19, :mri_20, :rbx]
  gem 'guard-bundler'
  gem 'guard-rails'
  gem 'quiet_assets'
  gem 'rails_layout'
  gem 'rb-fchange', :require=>false
  gem 'rb-fsevent', :require=>false
  gem 'rb-inotify', :require=>false
end

group :test do
  gem 'email_spec', '>= 1.4.0'
  gem 'launchy', '>= 2.2.0'
  gem 'capybara', '>= 2.0.3'
  gem 'database_cleaner', '>= 1.0.0.RC1'
  gem 'cucumber-rails', '>= 1.3.1', :require => false
end

group :production do
  gem 'rails_12factor'
end

gem 'rspec-rails', '>= 2.12.2', :group => [:development, :test]
gem 'factory_girl_rails', '>= 4.2.0', :group => [:development, :test]
gem 'teaspoon', '>= 0.7.4', :group => [:development, :test]
gem 'cancan', '>= 1.6.9'
gem 'rolify', '>= 3.2.0'
gem 'stripe-rails'
gem 'faker'
gem 'open4'
gem 'unf'

当我在开发模式下运行它时,它运行得完全正常.当我通过生产模式(即使使用相同的配置文件)运行它时,它不会继承视图属性,如 digest_pathasset_digest_path is null 或 manifest> 等

When I run this in development mode, it runs completely fine. When I run this through production mode (even with the same config file) it does not inherit the View properties like digest_path or asset_digest_path is null or manifest, etc.

module Sprockets
  module Rails
    module Helper

      def compute_asset_path(path, options = {})
        if digest_path = asset_digest_path(path)
          path = digest_path if true # BUG: digest assets doesn't work on live, let's just bake it
          path += "?body=1" if options[:debug]
          File.join(assets_prefix || "/", path)
        else
          super
        end
      end

      def asset_digest_path(path, options = {})
        if manifest = assets_manifest
          if digest_path = manifest.assets[path]
            return digest_path
          end
        end

        if environment = assets_environment
          if asset = environment[path]
            return asset.digest_path
          end
        end
      end

    end
  end
end

module ActionView
  module Helpers
    module AssetUrlHelper
      def compute_asset_path(source, options = {})
        dir = ASSET_PUBLIC_DIRECTORIES[options[:type]] || ""
        File.join(dir, source)
      end
    end
  end
end

推荐答案

希望这能帮助我的程序员朋友避免一些头痛 :D

Hopefully this will help save my fellow programmer friends some head banging :D

我正在将文件上传到 S3,但我没有意识到 Rails 未加载清单.您可以正确设置所有生产设置(如上面和其他线程中),但如果您没有 Rails 可读的 manifest.json 文件,它仍会生成/javascript/*(示例)网址.

I was uploading the files to S3, I didn't realize that the manifest wasn't loaded by Rails. You can have all your production settings right (like above and in other threads), but if you don't have the manifest.json file readable by Rails, it will still generate /javascript/* (example) urls.

我在使用 multi_json gem 的最新版本时仍然遇到问题,所以我将它降级到 1.7.8 并且它工作正常.

I was still having trouble with multi_json gem's latest version, so I downgraded it to 1.7.8 and it works fine.

gem 'multi_json', '1.7.8'

这样它就可以读取 rake assets:precompile 创建的 manifest.json 文件.

That's so it can read the manifest.json file which rake assets:precompile creates.

关于这个链轮线程的争论 https://github.com/rails/sprockets-rails/issues/107 关于你的清单文件应该在 git 中还是只是在部署脚本中,做最适合你的,只要确保它可以在以下位置找到:

There is a debate on this sprockets thread https://github.com/rails/sprockets-rails/issues/107 on whether your manifest file should be in git or just on a deploy script, do what suits you best, just make sure it is findable in:

/public/assets/manifest.json 

或者自己指定

config.assets.manifest = '...'

这可能会或可能不会被贬低.

That may or may not be depricated.

干杯!

这篇关于摘要路径 &amp;asset_digest_path 不允许使用摘要 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆