使用宝石加载资产(麻烦不加载资产)

布拉德波特

我有一个经常在我的网站上使用的主题。我一直在尝试通过gem加载css,js和plugin文件夹(包含在主题中)资产。

Sprockets::FileNotFound: couldn't find file 'phcthemes' with type 'application/javascript'

如果我取出javascript(仅适用于CSS),该代码将不会执行,并且在已编译资产中会完全像这样显示

*= phcthemes

我的想法应用程序无法识别出宝石,我尝试使用isolate_namespace(目前已注释),还尝试了使用以下答案中的信息进行的几种不同的配置,但无济于事。

主应用程序application.css

*= phcthemes

主应用程序application.js

// require phcthemes

lib / phcthemers.rb

require "phcthemes/version"

module Phcthemes
    class MyRailtie < Rails::Railtie
    end

    class Engine < ::Rails::Engine

        # Isolate Namespace
        #isolate_namespace Phcthemes

        # Initialize Assets
        initializer :assets do |config|
            Rails.application.config.assets.precompile += %w{ application.js application.css }
            Rails.application.config.assets.paths << root.join("app", "assets", "javascripts")
            Rails.application.config.assets.paths << root.join("app", "assets", "stylesheets")
        end

    end

end

phcthemes.gemspec

$:.push File.expand_path("../lib", __FILE__)

# Maintain your gem's version:
require "phcthemes/version"

Gem::Specification.new do |spec|

    spec.name        = "phcthemes"
    spec.version     = Phcthemes::VERSION
    spec.authors     = ["BradPotts"]
    spec.email       = ["[email protected]"]
    spec.homepage    = "http://phcnetworks.net"
    spec.summary     = "PHCThemes"
    spec.description = "PHCNetworks theme with mtdevise and assets built in."
    spec.license     = "GPL-3.0"

    spec.files         = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
    spec.bindir        = "exe"
    spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
    spec.require_paths = ["lib"]

    spec.files = Dir["{app,config,db,lib}/**/*", "LICENSE", "Rakefile", "README.md"]

    # Main Structure & Security
    spec.add_dependency "rails", "~> 4.2.5.2"

    # Development & Testing
    spec.add_development_dependency "sqlite3"
    spec.add_development_dependency "bundler", "~> 1.11"
    spec.add_development_dependency "rake", "~> 10.0"
    spec.add_development_dependency "rspec", "~> 3.0"

end
最大净化器

您的Engine类中缺少一些代码。包括以下这些

initializer :assets do |config|
  Rails.application.config.assets.precompile += %w{ myfile.js myfile.css }
  Rails.application.config.assets.paths << root.join("app", "assets", "javascripts")
  Rails.application.config.assets.paths << root.join("app", "assets", "stylesheets")
end

然后将文件添加到gem目录中的app / assets / javascripts或app / assets / stylesheets中。如果它对于供应商目录或任何其他目录都无法正常工作,我会感到惊讶。

当gem安装到Rails应用程序时,您仍然需要在application.css和application.js中放入require语句。顺便说一句,您的CSS需求陈述不正确-应该是*= require phcthemes

Railtie文档说一个Railtie类需要运行初始化的时候,所以包括你的主要宝石模块内的以下类:

class MyRailtie < Rails::Railtie
end

那应该做。实际上,您根本不需要更改gemspec。

您可以查看我的socket_helpers gem作为示例。这是我遇到相同问题时正在构建的东西。

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章