【问题标题】:Load Assets Using a Gem (Having Troubles Won't Load Assets)使用 Gem 加载资产(遇到问题不会加载资产)
【发布时间】:2016-03-02 04:42:53
【问题描述】:

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

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

如果我取出 javascript(仅限 css),它不会执行并在编译的资产中完全像这样显示

*= phcthemes

我的想法应用程序无法识别 gem,我尝试使用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       = ["bradley.j.potts@gmail.com"]
    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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 rubygems asset-pipeline


    【解决方案1】:

    您的 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 类,因此在主 gem 模块中包含以下类:

    class MyRailtie < Rails::Railtie
    end
    

    应该这样做。您实际上根本不必更改 gemspec。

    您可以查看我的socket_helpers gem 作为示例。当我遇到同样的问题时,这就是我正在构建的。

    【讨论】:

    • 感谢 Max 的回答,看起来它应该可以工作,但由于某种原因对我不起作用。顺便说一句,感谢您的项目链接真的帮助了我。我用我所做的附加信息和更改更新了我的问题。将继续努力。
    • @bradpotts 看起来您问题中的 css/js 要求语句仍然是错误的,它应该是 *= require phcthemes//= require phxthemes。当没有等号时,表示未加载依赖项。在 CSS 和 JS 中,等号后的“require”也是必需的
    • 感谢您的所有帮助。我做了改变。我仍然有同样的错误,但我很满意,因为它看起来是正确的,并让我朝着正确的方向前进。如果您发现任何其他问题,请告诉我。
    • 是的。这是链接,如果您想查看它。 github.com/bradpotts/phc-themes
    • @bradpotts 我分叉了你的 gem,修复了你的问题,并提出了 pull request。请参阅拉取请求上的评论以了解我做了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多