2016-03-02 112 views
2

我有一个主题,我经常在我的网站上使用。我一直试图通过一个宝石加载CSS,JS和插件文件夹(包含在主题中)。使用宝石加载资产(遇到问题不会加载资产)

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 

回答

2

您的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 

,然后包括在你的宝石目录应用程序/资产/ Java脚本或应用/资产/样式表文件。如果它对供应商目录或任何其他目录不起作用,我会感到惊讶。

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

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

class MyRailtie < Rails::Railtie 
end 

这应该做到这一点。你根本不需要改变gemspec。

例如,您可以看到我的socket_helpers宝石。当我遇到同样的问题时,这就是我正在建立的。

+0

感谢您的答案马克斯,它看起来应该工作,但不因为某种原因与我。顺便说一句,感谢您的项目链接真的帮助我。我更新了我提出的更多信息和更改。将继续在此工作。 – bradpotts

+0

@bradpotts它看起来像你的问题中的css/js需要语句仍然是错误的,它应该是'* = require phcthemes'和'// = require phxthemes'。如果没有等号表示依赖项未被加载。等号后的'require'这个词也是必需的,无论是在CSS还是JS –

+0

谢谢你的帮助。我做了改变。我仍然有同样的错误,但我感到满意,因为它看起来是正确的,并使我朝着正确的方向前进。如果你注意到其他事情,请让我知道。 – bradpotts