2016-03-02 110 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